简体   繁体   English

使用htaccess在php中清理URL

[英]Clean url in php using htaccess

Clean url in php using htaccess 使用htaccess在php中清理URL
Here is an example 这是一个例子

http://www.example.com/Mobiles/index.php?idd=4 http://www.example.com/Mobiles/index.php?idd=4

and i want result like this 我想要这样的结果

http://www.example.com/Mobiles http://www.example.com/Mobiles

Please help 请帮忙

This is called URL Rewrite . 这称为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. 如果您的页面链接是动态的,例如在电子商务站点中大多数情况下是从数据库中提取数据,那么最好的方法是将id附加在URL的末尾。 This way you can fetch the data from database. 这样,您可以从数据库中获取数据。 Like in your case, your new URL might look like: 就像您的情况一样,您的新URL可能类似于:

http://www.example.com/Mobiles/4

When user will visit this link .htaccess file will internally rewrite this URL to: 当用户访问此链接时, .htaccess文件将在内部将该URL重写为:

http://www.example.com/Mobiles/index.php?id=4

In this way you can then retrieve id from your PHP like this: 这样,您就可以像这样从PHP中检索id

$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. extract函数将根据参数名称自动创建变量,您可以直接使用$id变量进行访问。

Here is the .htaccess code: 这是.htaccess代码:

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: 如果您不需要像http://www.example.com/Mobiles/4这样的URL,请使用以下代码:

RewriteEngine On
RewriteRule ^Mobiles$ http://www.example.com/Mobiles/index.php?id=4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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