简体   繁体   English

将HTTPS重定向与WWW重定向合并

[英]Merging a HTTPS redirect with WWW redirect

I currently have this in my .htaccess file 我目前在我的.htaccess文件中有此文件

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

This ensures all http://www.example.com is redirected to http://example.com 这样可以确保将所有http://www.example.com重定向到http://example.com

I would like to implement an SSL certificate and I found this is the rewrite rule I must use for http to redirect to https. 我想实现一个SSL证书,我发现这是我必须用于http重定向到https的重写规则。

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

How could I merge the HTTPS condition with my current condition so that 如何将HTTPS条件与当前条件合并,以便

http://www.example.com redirects to https://example.com http://www.example.com重定向到https://example.com

http://example.com redirects to https://example.com http://example.com重定向到https://example.com

If it is not possible to merge this, can I just have those two code snippets one after another? 如果无法合并此代码,是否可以将这两个代码片段一个接一个地合并?

Thanks in advance. 提前致谢。

Yes, you can use them one after another. 是的,您可以一个接一个地使用它们。 I'd also save your users a redirect by having your first set of rules send them straight to HTTPS. 通过将您的第一组规则直接发送给HTTPS,我还将为您的用户节省重定向。

RewriteEngine On
RewriteBase /

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

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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

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