如何确保所有htaccess重写规则都能正常工作

时间:2015-07-09 20:56:53

标签: .htaccess mod-rewrite

我的htaccess文件中有以下代码

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteRule ^(online-shop)/?$ $1/home [L,NC]
RewriteRule ^(my-integra)/?$ $1/home [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.+)/?$ index.php?id=$1 [QSA,NC]

RewriteRule ^(blog)/(post|tags)/([\w-]+)/?$ index.php?id=$1&type=$2&unique=$3 [QSA,NC]

RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ index.php?id=$1&type=$2&year=$3&month=$4 [QSA,NC]

底部的两条规则似乎不起作用,如果我访问domain.com/blog/post/post-1它没有显示正确的网页而只显示index.php?id=blog/post/post1而不是index.php?id=blog&type=post&unique=post1

1 个答案:

答案 0 :(得分:1)

RewriteCond仅适用于下一个RewriteRule。同时将通用规则作为最后一条规则。

这样做:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteRule ^(online-shop)/?$ $1/home [L,NC]
RewriteRule ^(my-integra)/?$ $1/home [L,NC]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^(blog)/(post|tags)/([\w-]+)/?$ index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]

RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ index.php?id=$1&type=$2&year=$3&month=$4 [QSA,NC,L]

RewriteRule ^(.+?)/?$ index.php?id=$1 [QSA,L]