【发布时间】:2016-07-27 09:15:09
【问题描述】:
不幸的是,我遇到了卡住的问题。
我会在每个 URL 后面添加一个斜杠,然后通过 301 将不带斜杠的版本重定向到带有斜杠的变体。
以前,我已经成功删除了带有 url 重写的 html 文件扩展名并实施了 SSL。
所以,我尝试使用以下代码添加斜杠。
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
我在这个线程上找到了这个解决方案:Htaccess: add/remove trailing slash from URL
不幸的是,它仍然无效。
有趣的是,例如,当我访问以下 URL(末尾带有斜杠)时:
example.com/imprint/
我收到以下消息:在此服务器上未找到请求的 URL /imprint.html/。
imprint.html???
这是我完整的 .htaccess 文件:
RewriteEngine On
#Activate and force ssl and redirect from non-www to www#
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
#Removes HTMl-Extension and make URLs clean#
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
#Redirect Index Files e.g. /index or /index.html
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
#Adds a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
我希望你能帮助我。 :-)
【问题讨论】:
标签: apache .htaccess redirect mod-rewrite url-rewriting