删除html扩展并在所有目录上的文件中添加尾部斜杠,而不会影响内部URL

时间:2013-03-18 16:36:41

标签: .htaccess mod-rewrite redirect trailing-slash canonicalization

我正在使用下面的代码将index.html和非www版本的URL重定向到www。它还从文件中删除* .html扩展名。现在,我想在所有目录的文件末尾添加一个尾部斜杠。以下是我想要的例子:

  • www.mydomain.com.au/contact.html访问www.mydomain.com.au/contact /
  • www.mydomain.com.au/contact转到www.mydomain.com.au/contact /
  • www.mydomain.com.au/glass-replacement/Brisbane.html去了 /玻璃 - 替换/布里斯班/

依此类推......

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^.*\/index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
RewriteCond %{http_host} ^mydomain.com.au$ [nc]
RewriteRule ^(.*)$ http://www.mydomain.com.au/$1 [r=301,nc,L]

RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.html)?(\?.*)?\  [NC]
RewriteRule ^(.+/)?index(\.html)?$ /%1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]

感谢您的提前帮助

1 个答案:

答案 0 :(得分:0)

试试这个:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^.*\/index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
RewriteCond %{http_host} ^glassnow.com.au$ [nc]
RewriteRule ^(.*)$ http://www.glassnow.com.au/$1 [r=301,nc,L]

RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.html)?(\?.*)?\  [NC]
RewriteRule ^(.+/)?index(\.html)?$ /%1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.glassnow.com.au/$1/ [R=301,L]