简体   繁体   中英

how to rewrite a URL using PHP and.htaccess with multiple values [on hold]

newprojectinfo.com/property-single.php?city=Bangalore&area=Rajaji-Nagar&project_name=Prestige-West-Woods

need to rewrite that URL like:

newprojectinfo.com/Bangalore/Rajaji-Nagar/Prestige-West-Woods

let me know how to write .htaccess for this URL.

    //-----The below anchor tag is my code--------
   <a href="property-single.php?city=<?php echo rtrim(str_replace(' ', '-', $slideProjects['city_name']))?>&area=<?php echo str_replace('
', '-', $slideProjects['area_name'])?>&project_name=<?php echo
 str_replace(' ', '-', $slideProjects['project_name'])?>">

For the .htaccess you can try this, the second line is to escape any file type that I write in there.

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ index.php?p=$1

Then you can get the data like this.

$url         = $_SERVER['REQUEST_URI'];
$explode     = explode('/', $url);
$city        = $explode[1];
$area        = $explode[2];
$projectName = $explode[3];

The index 0 of $explode is empty because the $url will be like this /Bangalore/Rajaji-Nagar/Prestige-West-Woods

Edit: You need to add more data in your URL if you want your app to know when to access property-single.php or you can make it static and redirect all requests to property-single.php, maybe your URL will be like this
newprojectinfo.com/property-single/Bangalore/Rajaji-Nagar/Prestige-West-Woods
then you can include the file based on the index 1 of $explode array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM