htaccess的,以非www到www和HTTP重定向到https

问题描述:

我有这样的事情htaccess的,以非www到www和HTTP重定向到https

RewriteEngine on 
RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L] 

它工作正常,会将所有http://www.example.comhttps://www.example.com。但是现在我想让它将所有example.com重定向到https://www.example.com。我试过了:

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

RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L] 

它失败了。我应该添加什么?

您可以在一个单一的规则做到既重定向:

RewriteEngine on 

RewriteCond %{HTTP_HOST} !^www\. [NC,OR] 
RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] 
RewriteRule^https://www.%1%{REQUEST_URI} [R=302,NE,L] 
+0

感谢,但它没有工作:( –

+0

它适用于我的Apache的罚款。确保这是非常第一条规则,你完全清除浏览器缓存。 – anubhava

+0

我尝试了不同的浏览器和移动网络浏览器也。顺便说一句,它我使用openshift来托管我的应用程序 –

在.htaccess文件使用下面的代码会自动重定向访问者到你的网站的HTTPS版本:

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

或帮助与这个网站。最近我遇到同样的问题,他们解决这个问题。 https://www.themeconfig.com/wordpress-basic-services/

+0

谢谢,但我已经重定向到https,现在我想重定向非www到w ww也 –