如何使用.htaccess清理动态URL

时间:2014-07-17 11:08:04

标签: php .htaccess

我在htaccess中尝试过以下操作,但似乎无法正常工作,

Options +FollowSymLinks +Indexes +MultiViews

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ $1.php?bid=$2&page=$3 [L]

通过这样做,

http://www.turkish-property-world.com/antalya_apartment.php?bid=4&page=1

应该是

http://www.turkish-property-world.com/antalya_apartment/4/1

2 个答案:

答案 0 :(得分:1)

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)/$ $1.php?bid=$2&page=$3 [L,QSA]
RewriteRule ^(.*)/(.*)/(.*)$ $1.php?bid=$2&page=$3 [L,QSA]

答案 1 :(得分:1)

您的规则是正确的,但真正的问题是您使用MultiViews。把它拿出来用:

Options +FollowSymLinks +Indexes -MultiViews

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?bid=$2&page=$3 [L,QSA]
  • MultiViews使用选项Apache's content negotiation module,它在mod_rewrite之前运行,并使Apache服务器匹配文件扩展名。因此/file可以在网址中,但它会投放/file.php