htaccess重写问题添加目录

时间:2015-03-03 04:19:47

标签: .htaccess

我对这个htaccess写规则感到难过。

我目前有这条规则在运作:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /item\.php\?pid=([a-z0-9]+)\ HTTP/ [NC]
RewriteRule ^item\.php$ http://www.example.com/%1? [R=301,L]

RewriteRule ^([-a-z0-9]+)$ /item.php?pid=$1 [NC,L]

原始网址:

http://www.example.com/item.php?pid=2015-hyundai-genesis-coupe

启用此网址:

http://www.example.com/2015-hyundai-genesis-coupe

我需要的是这样的网址:

http://www.example.com/item/2015-hyundai-genesis-coupe

我尝试了一堆没有成功的事情。有谁熟悉这个?

谢谢!

1 个答案:

答案 0 :(得分:0)

将htaccess改为:

Options -Multiviews

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /item\.php\?pid=([a-z0-9]+)\ HTTP/ [NC]
RewriteRule ^item\.php$ http://www.example.com/item/%1? [R=301,L]

RewriteRule ^item/([-a-z0-9]+)$ /item.php?pid=$1 [NC,L]

您可能需要也可能不需要Options行。这样可以确保mod_negotiation不会取代mod-rewrite并将/item映射到/item.php,然后mod_rewrite可以重写请求。

此外,您可能需要确保在item.php页面中有绝对链接,否则他们将无法工作,因为您通过改为相对URI基础来改变相对URI基础/item/ /。您也可以将其添加到页面标题中:

<base href="/" />