【问题标题】:htaccess url rewriting: adding trailing slash does not workhtaccess url重写:添加斜杠不起作用
【发布时间】: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


    【解决方案1】:

    当您检查!-f(不是文件)时,它也会跳过.html 文件。你可以使用[OR]

    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]
    
    #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,NE,NE]
    
    #Adds a trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*[^/])$ /$1/ [L,R=301,NE]
    
    #Removes HTMl-Extension and make URLs clean# 
    
    #example.com/page will display the contents of example.com/page.html
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.+?)/?$ $1.html [L]
    

    【讨论】:

    • 谢谢,但这不起作用。 :-( 相同的错误 example.com/imprint 说:404 - example.com/imprint.html/ 不存在。
    • 不幸的是,它对我不起作用。没有变化,只有 404。:
    • 现在试试我的更新答案。还要告诉你使用什么 URL 来测试?
    猜你喜欢
    • 1970-01-01
    • 2012-05-20
    • 2017-07-16
    • 1970-01-01
    • 2015-03-19
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多