2013-05-03 167 views
0

我正在尝试向我的.htaccess添加一些代码,以将斜杠和非斜杠网址重定向到除我的主页外的.html所有网址。.htaccess 301将非斜杠域名和斜杠域名重定向到.html

例如 www.mydomain.com/cat/和www.mydomain.com/cat 应该重定向到www.mydomain.com/cat.html

我已成功地添加以下到我的。 htaccess的重定向其到www.mydomain.com/cat正确的地方www.mydomain.com/cat.html但需要就如何使斜线版本重定向到的.html页面

RewriteCond %{REQUEST_URI} !\.[^./]+$ 
RewriteCond %{REQUEST_URI} !(.*)/$ 
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L] 

我的整个的.htaccess一些帮助看起来像这样,如果任何人有任何建议,应该如何看待上述这将不胜感激。

Options +FollowSymlinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^xxx.xxx.xxx.xx [nc,or] 
RewriteCond %{HTTP_HOST} ^mydomain.com [NC] 
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301] 

RewriteCond %{THE_REQUEST} ^.*/index.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L] 


RewriteCond %{REQUEST_URI} !\.[^./]+$ 
RewriteCond %{REQUEST_URI} !(.*)/$ 
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L] 


DirectoryIndex index.html index.php 

<IfModule mod_rewrite.c> 
RewriteEngine on 
# Pleas note that RewriteBase setting is obsolete use it only in case you experience some problems with SEO addon. 
# Some hostings require RewriteBase to be uncommented 
# Example: 
# Your store url is http://www.yourcompany.com/store/cart 
# So "RewriteBase" should be: 
# RewriteBase /store/cart 
# RewriteBase/
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . index.php?sef_rewrite=1 [L,QSA] 

</IfModule> 

解决: 我只是说:

RewriteCond %{REQUEST_URI} ^(.+)/$ 
RewriteRule ^(.+)/$ /$1 [R=301,L] 

回答

0

单独的规则似乎是为你工作,但我认为你可以将其简化为一个规则,一个可选的斜杠。您的规则将斜线重定向到无斜线,然后再重定向到.html。有一条规则,你只有一个重定向。

这有标准的RewriteCond检查它是否不是文件或文件夹,所以它不会保持重定向.html,如果它已经是一个。然后,ReweriteRule中的\?是可选的斜杠。

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/?$ http://mydomain.com/$1.html [R=301,L] 

如果这都在你的域名,你可以从结果忽略它:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/?$ /$1.html [R=301,L] 

此外,请注意这将赶上和子文件夹时,您是否并不意味着它。例如, www.mydomain.com/animals/cat/将重定向至www.mydomain.com/animals/cat.html