Mobile app version of vmapp.org
Login or Join
Looi9037786

: Force https and no www using htaccess This question has been asked a million times before but my problem is that this still doesn't work for me, regardless of what code I use so I was wondering

@Looi9037786

Posted in: #Htaccess #Https #NoWww #Redirects

This question has been asked a million times before but my problem is that this still doesn't work for me, regardless of what code I use so I was wondering if I could have some help perhaps?

This is my current htaccess and it does successfully remove the www, but the https doesn't work at all. I want it to redirect all traffic to example.com if the requested url uses www or http. Any help please? Thanks a bunch!

RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} !^example.com
RewriteRule ^(.*)$ example.com/$ [L,R=301]



note: the rewrite engine is on, it's a few lines above.

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Looi9037786

3 Comments

Sorted by latest first Latest Oldest Best

 

@Welton855

I've tried many different configs and rules, the following is the only bulletproof solution that works for me.

# match any URL with www and rewrite it to https without the www
RewriteCond %{HTTP_HOST} ^(www.)(.*) [NC]
RewriteRule (.*) %2%{REQUEST_URI} [R=301,L]

# match non https and redirect to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ %{HTTP_HOST}/ [R=301,L]


The order matters, as it will prevent a 3rd redirect in some cases.

For a subdomain, you just use

# match non https and redirect to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ %{HTTP_HOST}/ [R=301,L]


I use Cloudways coupled with Cloudflare and this works perfectly, all other provided examples caused a redirect loop or failed to resolve..

10% popularity Vote Up Vote Down


 

@Jessie594

You can also use the following:

RewriteCond %{HTTPS} off
RewriteRule .* %{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^domain-name.tld$ [NC]
RewriteRule ^(.*)$ domain-name.tld/ [L,R=301]

10% popularity Vote Up Vote Down


 

@Annie201

Use this. It should save you from two headaches.

RewriteEngine On
RewriteCond %{HTTP_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ example.com/ [R=301,L]


this also allows any URL that starts with www.example.com/ or that connects to port 80 (the standard web port) to redirect to example.com/

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme