www to non-www & http to https & include 尾部斜杠



我的.htaccess文件需要帮助。我想要一个301重定向,从HTTP到HTTPS,从www到非www,最后加一个斜杠。我不能从帖子中删除尾部斜杠,因为WordPress在URL的末尾添加了它,当我删除它时,我所有的帖子都返回404错误,有些插件不起作用。

我以这样的结果结束:

  • http://www.example.com/post->https://example.com/post->https://example.com/post/
  • http://example.com/index.php->https://example.com/index.php->https://example.com/

我是这个论坛的新手,所以我不能添加超过8个链接,所以我做了一个包含更多细节的截图。在此处输入图像描述

Current.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重定向?

有这样的规则:

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]

最新更新