.htaccess 301通过htaccess将index.html和所有http、https、www和非www版本的主页URL重定向到https://www根URL

6pp0gazn  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(162)

我想通过htaccess文件301重定向以下URL
这意味着我需要强制SSL(https)版本,并强制整个网站的www。
另外我还想301重定向index.html到https和www版本的主页网址。
我想301重定向以下URL:

https://www.example.com/index.html
https://example.com/index.html
https://example.com
http://www.example.com/index.html
http://example.com/index.html
http://www.example.com
http://example.com
www.example.com
example.com

结束日期

“https://www.example.com”

除此之外,我需要将该网站的所有URL重定向到https://www版本,如下所示:

“https://www.example.com/new-site.html”

下面是我现在使用的:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
RewriteRule .* ? [F,L]
RewriteBase /
RewriteRule ^index\.html$ / [NC,R,L]

但是上面的规则似乎给予了我一个301---〉302----200 ok重定向2个url的/index. html和301为其余的。
它给出了302,来自:

“https://www.example.com/index.html”

到这

“https://www.example.com/“
  • 谢谢-谢谢
o7jaxewo

o7jaxewo1#

您可以在一个规则中完成所有这些操作:

RewriteEngine On

## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} /index\.html$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule (?:^|(.+)/)(?:index\.html)?$ https://www.%1/$1 [R=301,L,NE]
uinbv5nw

uinbv5nw2#

好吧,我自己想明白了:
下面是它的工作原理,只需要在最后一条规则中添加R=301,而不仅仅是R

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
RewriteRule .* ? [F,L]
RewriteBase /
RewriteRule ^index\.html$ / [NC,R=301,L]

相关问题