.htaccess redirects not working after changing to www

In order to get Cloudflare CDN working on my site, I had to change my domain to a www. My hosting provider told me to update my .htaccess to accomplish this. The Cloudflare began working with the update. A few weeks later, I now note that the 301 redirects are no longer working. If I change back to my older code, it works. I am hoping that someone can see the error in the code that is preventing the redirects. Thank you.

Old code:

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

New Code: (CDN works, but redirects don’t)

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

I tried the link and it appears ok?

All Redirects to:

Tested here:
https://supiet2.tk

Thanks for taking a look.
If you enter an old page or a mispelling, you get a 404.

Redirect 301 /http://mrpitbull.com/samoa.html  https://www.mrpitbull.com/brindle.html

Okay, got it working with help from my provider A2 Hosting. Made a small adjustment in the www. redirect and then simplified my code for each redirect.

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

Example:

Redirect 301 /bandit.html /pocket-pitbull.html

Thanks for the help.

1 Like

There is still a problem because the URL returns:

too many HTTP redirects

I would try and disable The Cloudfare cachee and if that does not resolve the problem then disable Cloudfare and check every link to ensure redirection is correct.

Afterwards try Cloudfare again.

Out of the 100 plus redirects, that is as near as I can tell the only problem. I had that page redirecting to itself in the .htaccess.

I ran a broken link checker on the entire site and did not find another problem.

Thanks a bunch for the heads up. Who knows how long that would have went on before I noticed it.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.