繁体   English   中英

如何使用.htaccess将http:// http:// www和https:// www重定向到https://

[英]How to redirect http:// http://www and https://www to https:// using .htaccess

我试图做一个通配符重定向http://example.comhttp://www.example.comhttps://www.example.comhttps://example.com

我尝试了这里提到的解决方案但它不适合我,因为当我打开任何页面时,Web浏览器显示太多重定向错误。

我最接近的是我可以使用以下代码将http://example.comhttp://www.example.com重定向到https://example.com 但仍然无法弄清楚如何做https://www.example.comhttps://example.com重定向

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

注意(可能这是导致重定向过多的问题):
我刚刚注意到,即使没有在.htaccess中指定任何重定向代码(请参阅下面的代码), http://www.example.com也会被重定向到http://example.com

<IfModule mod_rewrite.c>

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

试试:

RewriteEngine on
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^www
RewriteRule ^.*$ https://example.com%{REQUEST_URI} [R,L]
#wordpress rules

我刚刚弄明白了这个问题。 SSL证书刚刚为https://example.com创建。 我使用example.comwww.example.com分配了一个新的多域SSL证书,现在事情按预期工作了。 这是我工作的.htaccess文件:

RewriteEngine on

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

RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

<IfModule mod_rewrite.c>

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM