Clean url in php using htaccess
Here is an example
http://www.example.com/Mobiles/index.php?idd=4
and i want result like this
http://www.example.com/Mobiles
Please help
This is called URL Rewrite
. If your page links are dynamic, like extracting data from database which is mostly the case in e-commerce sites, then the best approach is to append the id
at the end of URL. This way you can fetch the data from database. Like in your case, your new URL might look like:
http://www.example.com/Mobiles/4
When user will visit this link .htaccess
file will internally rewrite this URL to:
http://www.example.com/Mobiles/index.php?id=4
In this way you can then retrieve id
from your PHP like this:
$id = $_GET['id'];
or:
extract($_GET);
This extract
function will create the variables automatically from the parameters name and you can access it directly with $id
variable.
Here is the .htaccess
code:
RewriteEngine On
RewriteRule ^Mobiles/(\d+)$ http://www.example.com/Mobiles/index.php?id=$1
In case if you don't need URL like http://www.example.com/Mobiles/4
, then use this:
RewriteEngine On
RewriteRule ^Mobiles$ http://www.example.com/Mobiles/index.php?id=4
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.