Reputation: 2174
I know there are a lot of questions on stack overflow regarding this already, but none of the solutions I find seem to work properly.
I have set up an SSL cert that requires the domain it's on to be non-www.
I want to redirect a user visiting my site to https://example.com no matter where they are coming from:
http://example.com
becomes https://example.com
http://www.example.com
becomes https://example.com
https://www.example.com
becomes https://example.com
Should be simple but unfortunately I have no .htaccess knowledge. I appreciate any help!
Upvotes: 1
Views: 423
Reputation: 3329
Below ruleset is fulfilling all below 6 cases:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
Cases:
C1: http://example.com/
C2: http://www.example.com/
C3: https://www.example.com/
C4: http://example.com/LineDemo/
C5: http://www.example.com/LineDemo/
C6: https://www.example.com/LineDemo/
Upvotes: 0
Reputation: 785246
You can use this code in your DOCUMENT_ROOT/.htaccess
file:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,NE,L]
Upvotes: 1