AllThisOnAnACER
AllThisOnAnACER

Reputation: 331

htaccess and regex redirects for multiple styles

I want to redirect all of the following

example.com
http://example.com
https://example.com
http://www.example.com

to

https://www.example.com

I've got this:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^/(.*):SSL$   https://%{SERVER_NAME}/$1 [R,L]


Options +FollowSymlinks
RewriteCond %{HTTP_HOST} !^www.example.com
RewriteRule ^(.*)$ https://www.example.com/$1 [R=permanent,L]

These two below are working,

http://www.example.com 
https://example.com 

but the last one shows up as:

https://www.example.com/https://example.com/

How can I correct this so all three redirect properly to https://www.example.com

Thanks for your help.

Upvotes: 1

Views: 39

Answers (2)

anubhava
anubhava

Reputation: 785196

All that you want can be done in a single rule like this:

RewriteEngine on

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

Make sure this is very first rule in your root .htaccess.

Upvotes: 1

vks
vks

Reputation: 67968

^(?:.*?:)?(?:\/\/)?(?:.*?\.)?(.*?\.)(.*)$

Try this.Replace by

https://www.$1$2

See demo.

http://regex101.com/r/lU7jH1/6

Upvotes: 0

Related Questions