busy1
busy1

Reputation: 43

Redirect fails to remove www

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

I'm trying force everything to https and eliminate www. I know this has been hashed over and over - but I'm trying to understand why this version - in particular - is not working. When I add the above code to htaccess, the force https works great - but it does not remove the www. How is that even possible given the explicit url that I'm using in the rewriterule?? I've tried removing cache, different browser, different machine, etc. Could there be something else at the server level overriding it? tx

Upvotes: 2

Views: 68

Answers (1)

anubhava
anubhava

Reputation: 785481

You can use this rule to do both i.e. http->https and remove www:

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

Make sure to keep this rule as first rule and clear your browser cache.

Upvotes: 1

Related Questions