【问题标题】:Removing html extentions and add trailing slash to files on all directories without affecting internal urls删除 html 扩展名并在所有目录上的文件中添加斜杠而不影响内部 url
【发布时间】:2013-03-18 16:36:40
【问题描述】:

我正在使用下面的代码将 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]

提前感谢您的帮助

【问题讨论】:

  • 如果用户只请求/foo,你想重定向到/foo/
  • @CBroe 是的。没错。

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


【解决方案1】:

试试这个:

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]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 2011-12-11
    • 2012-03-18
    相关资源
    最近更新 更多