Question

Here is a link to an almost identical question htaccess for redirecting non-www to www while preserving http & https

How would this be written for the opposite - I want to redirect www to non-www while preserving http or https, whatever the link may be originally

Was it helpful?

Solution 2

This was what worked, after some trial and error:

# Make all http use https and force non-www
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://mysite.ca%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

Sorry for not coming back to post this sooner, I'm learning...

OTHER TIPS

You can have these 2 rules:

RewriteEngine On

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

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top