【问题标题】:Trailing slash not added with .htaccess.htaccess 未添加尾部斜杠
【发布时间】:2018-03-09 18:27:46
【问题描述】:

我有以下 .htaccess 设置:

RewriteEngine On
RewriteBase /
DirectoryIndex index.php
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

我想要达到的目标:

  1. 从 http 重定向到 https
  2. 从非 www 重定向到 www
  3. 添加斜杠

前两点没问题,重定向按预期工作,但第三点没有。

如何调整现有规则以添加斜杠。

【问题讨论】:

    标签: .htaccess trailing-slash


    【解决方案1】:

    您可以在 .htaccess 中使用这些规则:

    DirectoryIndex index.php
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
    
    RewriteCond %{HTTPS} !on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
    
    # add a trailing slash to non-files
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
    

    确保使用新的浏览器来测试更新的规则。

    【讨论】:

    • 按照建议实施了新规则,并尝试了使用新浏览器的链接,但它似乎不起作用。仍然没有添加斜杠。
    • 很遗憾,我无法在此处粘贴 URL。
    • 好的,那么网址是这样的:example.com/articles/the-title-of-the-article-758
    • 这是新的,因为您从未告诉过任何 404?您在浏览器中输入导致 404 的 URL 是什么?还用您的完整 .htaccess 更新您的问题?
    • 添加不带斜杠的 URL 时出现 404。
    猜你喜欢
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    相关资源
    最近更新 更多