NewToCode
NewToCode

Reputation: 61

https non www redirect

I'm trying to get all my URLs to redirect to https:// without www.

If someone puts in www. or http:// or http:// they are redirected to https:// the problem comes if they put https://www. then it doesn't redirect to https://website

I'm using the following in my htaccess, any suggestions?

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Upvotes: 2

Views: 889

Answers (1)

anubhava
anubhava

Reputation: 785156

This single rule can take care of both requirements:

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

Make sure to remove both of you shown rules and test in a new browser to avoid old cache.

Upvotes: 4

Related Questions