mod_rewrite www to no-www and http to https simultaneously

旧时模样 提交于 2019-12-24 11:10:57

问题


I currently have mod_rewrite in place with code I found to redirect all requests to the same URL without www.

What I'm trying to do now is configuring Apache mod_rewrite to do the following, without any wasted actions/additional redirects.

http:// to https://

http://www. to https://

I'm not familiar with it, but in pseudo code, http(s)://(www.)domain.com to https://domain.com for solely the URLs at the top level, excluding sub domains.


回答1:


You just need one rule for both conditions:

put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [NE,R=301,L]



回答2:


Yes, there is a way to combine these operations into a single rule:

RewriteCond %{HTTP_HOST} !=example.com [OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^/(.*) https://example.com/$1



回答3:


This one worked for me. I tried the one above but it created a redirect loop on older browsers.

RewriteCond %{SERVER_PORT} !^443$ 
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L] 

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


来源:https://stackoverflow.com/questions/24593048/mod-rewrite-www-to-no-www-and-http-to-https-simultaneously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!