Pathros
Pathros

Reputation: 10730

.htaccess redirect from www.domain.com to https://domain.com

I have struggling and searching with no luck.

What I want is to redirect from www.domain.com to https://domain.com.

I have successfully solved for domain.com to https://domain.com by setting the following .htaccess as folows:

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

I have tried this answer and the www.domain.com does not redirect to https://domain.com, it only stays www.domain.com and it doesn't display anything.

How do I fix this?

Upvotes: 1

Views: 140

Answers (1)

anubhava
anubhava

Reputation: 785196

You can use this single rule to do both http -> https and www removal:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

Make sure to clear your browser cache before testing.

Upvotes: 1

Related Questions