【问题标题】:.htaccess - www to non-www & http to https & include trailing slash.htaccess - www 到非 www & http 到 https & 包括斜杠
【发布时间】:2021-03-30 02:29:34
【问题描述】:

我的 .htaccess 文件需要帮助。我希望有一个从 HTTP 到 HTTPS 以及从 www 到非 www 的 301 重定向,末尾带有斜杠。我无法从帖子中删除尾部斜杠,因为 WordPress 将它添加到 URL 的末尾,当我删除它时,我的所有帖子都返回 404 错误,并且某些插件不起作用。

我以如下结果结束:

我是这个论坛的新手,所以我不能添加超过 8 个链接,所以我制作了一个包含更多详细信息的屏幕截图。 enter image description here

当前.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

有什么办法可以去掉粗体部分,只有一个 301 重定向?

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    有这样的规则:

    RewriteEngine On
    
    # add a trailing slash with www removal and https
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !/$
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule [^/]$ https://%1%{REQUEST_URI}/ [L,NE,R=301]
    
    # handle www->non www, http->https
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
    
    RewriteRule ^index\.php$ - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-22
    • 2017-12-28
    • 2021-03-25
    • 1970-01-01
    • 2014-12-16
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多