.htaccess重写更改错误网址

时间:2014-06-18 16:13:27

标签: .htaccess mod-rewrite

我在.htaccess中制作了一些重定向内容,它运行得很安静。

RewriteEngine On
# add www. if missing
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# redirect index.html and index.php to the simple homepage
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\|/index\.html\ HTTP/
RewriteRule ^index\.php$|^index\.html$ http://www.example.com/ [R=301,L]
# add trailing slash if missing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]
# redirect simulated directories to php files of the same name in /content/
RewriteCond %{REQUEST_URI} !^(\/css|\/images) [NC]
RewriteRule ^([^/]+)/$ content/$1\.php?rw=1 [QSA,L]
RewriteCond %{QUERY_STRING} !^rw=1
RewriteRule ^content/([^/]+).php$ /$1/ [R=301,L]

但是虽然启用了添加尾部斜杠的部分,但是存在一个奇怪的问题,即不存在并且指向我的404错误页面的网址: 例如,如果我输入www.example.com/notexisting/,则将url转换为www.example.com/content/notexisting.php/?rw=1 当我在htaccess中禁用尾随斜杠的部分或模拟目录的部分时,不会发生这种情况。但我想保留两者。有可能吗?

1 个答案:

答案 0 :(得分:0)

看起来您可以通过在“斜杠”重定向中添加额外条件来轻松解决问题。您似乎没有任何以.php结尾的网址,因此您可以检查您的网址是否已经结束。如果不是这种情况,可以添加斜杠。

RewriteEngine On

# add www. if missing
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

# redirect index.html and index.php to the simple homepage
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\|/index\.html\ HTTP/
RewriteRule ^index\.php$|^index\.html$ http://www.example.com/ [R=301,L]

# add trailing slash if missing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^.*[^/]$ /$0/ [L,R=301]

# redirect simulated directories to php files of the same name in /content/
RewriteCond %{REQUEST_URI} !^(\/css|\/images) [NC]
RewriteRule ^([^/]+)/$ content/$1\.php?rw=1 [QSA,L]

RewriteCond %{QUERY_STRING} !^rw=1
RewriteRule ^content/([^/]+).php$ /$1/ [R=301,L]