1

I am redirecting 301 in cpanel www to non-www (naked domain) and the code Cpanel redirect generates doesn't do anything when www to non-www is set.

Server Type: (Litespeed Server, domain has "Let's encrypt" certificate.)

  • DNS config is (not pointing www to non-www as CNAME)
  • both point to same IP
  • mydomain.dev - A Record - 127.0.0.1
  • www.mydomain.dev - A Record - 127.0.0.1

Non-www to www - works

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.dev$
RewriteRule ^(.*)$ "https\:\/\/www\.mydomain\.dev\/$1" [R=301,L]

Www to non-www - doesn't work

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^www\.mydomain\.dev$
RewriteRule ^(.*)$ "https\:\/\/mydomain\.dev\/$1" [R=301,L]

Any ideas?

  • What do you mean by "doesn't work" exactly? Are you getting an error? A redirect loop? Nothing at all? "mydomain.dev - A Record - 127.0.0.1" - Is this really pointing to 127.0.0.1? Is your domain really a .dev TLD? – MrWhite Sep 2 at 14:13
1
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^www\.mydomain\.dev$
RewriteRule ^(.*)$ "https\:\/\/mydomain\.dev\/$1" [R=301,L]

This isn't a plain "www to non-www" redirect, it looks like you are also attempting to redirect HTTP to HTTPS (which you make no mention of in your question).

For an ordinary www to non-www redirect, you need to remove the first two conditions (like your earlier redirect that "works"). For example:

RewriteCond %{HTTP_HOST} ^www\.mydomain\.dev$
RewriteRule ^(.*)$ "https\:\/\/mydomain\.dev\/$1" [R=301,L]

However, you can tidy this up by removing the plethora of unnecessary backslash escapes in the RewriteRule substitution (typical cPanel). For example:

RewriteCond %{HTTP_HOST} ^www\.mydomain\.dev
RewriteRule ^(.*)$ "https://mydomain.dev/$1" [R=301,L]

I also removed the $ on the end of the preceding CondPattern - in order to catch a potentially FQDN.

You will need to clear your browser cache before testing. (Preferably test with 302 - temporary - redirects to avoid caching issues.)

  • Yes, I forgot to explicitly mention that I was redirecting to https as well in all cases with or without https and www I wanted it to land on naked https domain. What I mean by doesn't work is it simply doesn't redirect but stays to url you manually enter, except it did redirect http to https but www remained. – Manfred von Richthofen Sep 2 at 18:27
  • "redirecting to https as well in all cases with or without https" - OK, but the above would be incorrect if that is what you were trying to do as it "attempts" to redirect HTTP only. But where did you get X-Forwarded-SSL from? Were you advised to check this header by your host? Otherwise this looks like an error... you should never need to check both %{HTTPS} and %{HTTP:X-Forwarded-SSL}. (?) – MrWhite Sep 3 at 15:00
  • It was a Cpanel generated code. I'm not sure why it did that way. But looks like it's not reliable in all circumstances. – Manfred von Richthofen Sep 6 at 0:56
1

Try as follows. WITHOUT www (all visits go to https://example.com):

#start
RewriteEngine On
# non www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  • 1
    @dan thank you for clarification – Gabriel David Sep 3 at 13:37
  • I've fixed the original code by adding [OR] RewriteEngine on RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP:X-Forwarded-SSL} !on RewriteCond %{HTTP_HOST} ^www\.mydomain\.dev$ RewriteRule ^(.*)$ "https\:\/\/mydomain\.dev\/$1" [R=301,L] Thanks for all the help. – Manfred von Richthofen Sep 3 at 14:39
  • @ManfredvonRichthofen That is not strictly correct. Either HTTPS is always "off", in which case the check is redundant or the check for HTTP:X-Forwarded-SSL is redundant and you are allowing malicious requests to block your HTTP to HTTPS redirect. (?) – MrWhite Sep 4 at 10:56
  • Gabriel thanks. It works well. – Manfred von Richthofen Sep 6 at 1:06
  • Good to hear that! Regards! – Gabriel David Sep 6 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.