如何编写RewriteCond和RewriteRule来将url www更改为非www和http到https?

时间:2016-03-12 15:11:03

标签: redirect mod-rewrite rewrite httpd.conf url-rewrite-module

在Apache的httpd.conf文件中,如何重写模块以使url的www为非www并强制http为https? 当我在下面尝试时,它会变成无限重定向错误

RewriteCond %{HTTP_HOST} ^www.(.*)$
RewriteRule ^(.*)$ https://example.com$1

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ https://example.com$1

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

1 个答案:

答案 0 :(得分:0)

要从www / http重定向到非www / https,您可以使用:

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