Redirect from www to non-www (via .htaccess)

Hi everyone,

Like the title says, I am trying to figure out the best way to configure my htaccess file to redirect www URLs to non-www (I’ve noticed Google search chose to index some www page as canonical and some none, so thought this could help…?)

I’ve found this here, but it also redirects http to https, which my hosting provider already does, and thought too many redirect requests may cause issues…

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

I don’t really understand much about this so would appreciate your help on the best way to modify it! :pray:

depending on your hosting provider (especially if they’re already doing automatic https redirect) you might be able to configure the www subdomain to redirect to non-www without needing to use htaccess at all.

also, did you replace the .htaccess file that’s included with kirby with the snippet posted above? that might be the explanation if something doesn’t work

Hi @bruno, thanks for the reply! I didnt apply this yet in my htaccess since I saw its also redirecting https—>https, so thought I’d modify it first. Thought it shouldn’t be complicated.

The hosting support said I should check the Kirby documentation, but maybe they were being lazy :slight_smile: anyways, if there’s a correct way to modify it and remove the https redirect I would prefer it.

ok, i don’t have much to say then – i also don’t know much about htaccess. the snippet looks fine to me. if i understand it correctly, it only redirects if the page is accessed without https. Maybe somebody else has anything to say about it, otherwise I’d just apply it, see what happens, and if something breaks, revert your changes :man_shrugging:

Thanks @bruno!

Any ideas anyone?
:man_shrugging:t4:

Have you tried the rules above? It shouldn’t really matter, as these work conditionally.

I haven’t yet @texnixe. The hosting support suggested this may cause “too many redirects error”, so thought I’d ask here before…
So just to make sure I understand, this code above is conditional meaning if https is already pushed, it won’t apply?

Just for my own knowledge, would something simpler and clean like this work (found it also on one of your replies on another thread :slight_smile:) :

RewriteEngine On
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Yes, but it would make sense to redirect to https, not to http:

RewriteEngine On
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

But why not just try it? Even if you briefly run into a “too many redirects” issue, you can undo it again immediately.

2 Likes

True :slight_smile:
I’ll give it a go and thanks as always!
Just wanted to “understand” how it works I guess if that makes sense…