简体   繁体   English

mod_rewrite www到no-www和http到https同时

[英]mod_rewrite www to no-www and http to https simultaneously

I currently have mod_rewrite in place with code I found to redirect all requests to the same URL without www. 我目前使用代码找到了mod_rewrite,该代码可以将所有请求重定向到没有www的相同URL。

What I'm trying to do now is configuring Apache mod_rewrite to do the following, without any wasted actions/additional redirects. 我现在要尝试的是配置Apache mod_rewrite以执行以下操作,而不会浪费任何操作/其他重定向。

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. 我不熟悉它,但是使用伪代码将http(s)://(www.)domain.comhttps://domain.com ,仅获取顶级URL,不包括子域。

You just need one rule for both conditions: 对于这两种情况,您只需要一个规则:

put this code in your DOCUMENT_ROOT/.htaccess file: 将此代码放在您的DOCUMENT_ROOT/.htaccess文件中:

RewriteEngine On

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

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

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]

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

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