开发者

Apache http/https non-www to ww url redirection

开发者 https://www.devze.com 2023-02-05 12:58 出处:网络
I\'ve got a problem with apache configuration (linux distro). I want to redirect typed URLs (in browser):

I've got a problem with apache configuration (linux distro). I want to redirect typed URLs (in browser):

domain.com

www.domain.com

http://domain.com

http://www.domain.com

https://domain.com

to only one: https://www.domain.com Wha开发者_StackOverflowt should I do in .htaccess file? Notice that I've got only one certificate. Exactly for www.domain.com

Thank you in advance for any help! :)


This rule should do it:

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} (.+) [OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301]

The four conditions are two groups of disjunct conditions where the first condition of each group tests for a missing www. and the non-HTTPS respectively. Each second condition of the groups are just to get the part of the HTTP host without the leading www..


RewriteEngine On

RewriteCond %{HTTP_HOST}   !=www.domain.com [NC,OR]
RewriteCond %{HTTPS}       !=on [NC]
RewriteRule ^(.*)$         https://www.domain.com%{REQUEST_URI} [NE,R=permanent,L]

In any case, you only have one virtual host so you won't be able to skip the browser warning about the certificate when entering https://domain.com.

0

精彩评论

暂无评论...
验证码 换一张
取 消