将.php重命名为.html的htaccess规则正在干扰其他规则

时间:2013-05-08 13:47:39

标签: .htaccess mod-rewrite url-rewriting

我正在尝试将所有.php文件更改为.html扩展名,而我使用的规则正在干扰我的.htaccess文件中的其他规则。似乎我可以得到一套规则来工作或另一套规则。

检查代码:

Options +FollowSymLinks
RewriteEngine On

##Locations Pages
RewriteRule    ^([^/\.]+)\.html?$                           locations.php?locationslug=$1&Submit=Submit                 [L]

##Listing Pages
RewriteRule    ^([^/\.]+)\/([^/\.]+)\.html?$                listings.php?locationslug=$1&categoryslug=$2                [L]

##Vendor Pages
RewriteRule    ^([^/\.]+)\/([^/\.]+)\/([^/\.]+)\.html?$     vendors2.php?vendorslug=$3&locationslug=$1&categoryslug=$2  [L]

RewriteRule ^(.*)\.html$ $1.php [L]

如果我将行RewriteRule ^(.*)\.html$ $1.php [L]作为文件的第3行,则平面.php页面的工作方式为.html,但是,以下规则会引发错误(404 Not Found)。

如果我将行RewriteRule ^(.*)\.html$ $1.php [L]作为最后一行,那么平面页将无法用作.html。

与往常一样,非常感谢任何建设性的意见。提前谢谢!

1 个答案:

答案 0 :(得分:0)

尝试将这些规则的顺序从最具体的更改为最通用的规则,如下所示:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

##Vendor Pages
RewriteRule ^([^/]+)/([^/]+)/([^.]+)\.html?$ vendors2.php?vendorslug=$3&locationslug=$1&categoryslug=$2 [L,QSA,NC]

##Listing Pages
RewriteRule ^([^/]+)/([^.]+)\.html?$ listings.php?locationslug=$1&categoryslug=$2 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)\.html?$ $1.php [L,NC]

##Locations Pages
RewriteRule ^([^/.]+)\.html?$ locations.php?locationslug=$1&Submit=Submit [L,QSA,NC]

请注意,您的规则#3和4将匹配相同的URI模式。