删除所有php扩展并删除url中的变量

时间:2014-03-09 09:46:59

标签: php url mod-rewrite url-rewriting get

我得到了这个网址

/localhost/andalucia/productdetails.php?value=20

我想将其更改为

/localhost/andalucia/productdetails/20

你如何做到这一点以及如何在处理程序页面中获得值20?

我应该更改编码还是只添加一个ht访问文件?

如果要添加一个ht访问文件,应该包含哪些代码?

如果我有更多页面如下:

/localhost/andalucia/product
/localhost/andalucia/home
/localhost/andalucia/contactus

他们也会自动受到影响吗? 好吧我试着用

RewriteRule ^productdetails/([0-9]+)$ productdetails.php?value=$1 [L,QSA]

但现在问题是我的图片在html中消失了,我无法打开其他页面,如

/localhost/andalucia/product
/localhost/andalucia/home
/localhost/andalucia/contactus

我需要一个可以打开所有这些

的htaccess代码
/localhost/andalucia/product
/localhost/andalucia/home
/localhost/andalucia/contactus
/localhost/andalucia/productdetails/20

请帮助某人

1 个答案:

答案 0 :(得分:0)

使用apache扩展mod_rewrite,您可以轻松地将漂亮的URL转换为脚本所需的URL。您放在网络根目录中的这个.htaccess示例可以帮助您:

RewriteEngine On

RewriteRule ^andalucia/productdetails/([0-9]+)$ /andalucia/productdetails.php?value=$1 [L]

此示例仅重写andalucia/productdetails/NNNN...格式的网址,所有其他网址不会受到影响。如果需要传递其他查询参数,例如/andalucia/productdetails/20?sort=asc,则需要将QSA标志(查询字符串追加)传递给重写规则:

RewriteRule ^andalucia/productdetails/([0-9]+)$ /andalucia/productdetails.php?value=$1 [QSA,L]

L标志将禁止评估下一个规则。只需查看mod_rewrite文档即可进行深入讨论!