2014-06-18 78 views
0

我在我的.htaccess中做了一些重定向的东西,它工作安静。.htaccess重写更改错误url

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] 

不过,虽然添加尾随斜线部分启用有使用URL一个奇怪的问题不存在,并且是针对我的404错误页面: 例如,如果我进入www.example。 com/notexisting /那个URL被转换为www.example.com/content/notexisting.php/?rw=1 当我禁用尾部斜线部分或htaccess中模拟目录的部分时,不会发生这种情况。但我想保留两者。有没有可能?

回答

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] 
+0

完美!而已!非常感谢。 – konraed