简体   繁体   中英

Redirect to https not working with www

I have a domain called example.com .

I want to redirect always to https://www.example.com ( https AND www ).

Means: I need to redirect

https://example.com

http://www.example.com

http://example.com

to

https://www.example.com

This is the code I have so far:

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

This is workin for:

https://example.com

http://example.com

but it's NOT working for:

http://www.example.com

I'm working on this already the whole day (it's 4 pm here) but without success.

Why that? Who can give me a hint? What am I doing wrong?

The rule you are using only only redirects non-www domain to www without checking https header.

you can use this :

RewriteEngine on

#http to https with www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NC,L,R]
#other rules
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$  [NC]
RewriteRule !^web/ /web%{REQUEST_URI} [L,NC]

This will redirect both http or non-www to https://www

Clear your browser cache before testing this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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