Search code examples
apache.htaccesshttp-redirectmod-rewriteshared-hosting

htaccess rewrite to www except subdomain not working as expected


I know that similar questions have been asked, but this same code works on two sites and does not on a third, so I'm looking for ideas on what to change and/or what to check to figure out why it's not working on the third site.

What needs to happen: Redirect all requests on the domain that are (1) non-https to https, and (2) all non-www to www ... EXCEPT for the staging.___.com subdomain, which should receive neither www added to it, nor https.

#Rewrite http to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^staging\.(.+)$ [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]

#Rewrite non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} !^staging\.(.+)$ [NC]
RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}/$1 [R=301,L]

This works fine on two GoDaddy shared hosting sites ("Deluxe Web Hosting Linux") but NOT on the third which is on "Deluxe Linux Hosting with cPanel" if that matters. (Versions of PHP are different, Apache may be different but not sure how to check that since apache_get_version doesn't work on GoDaddy ...)

Where it redirects without the two !^staging lines in the htaccess:

http://www.staging.___.com/newtest/

and then it gets into an infinite redirect loop. ("newtest" is the subdomain folder specified in GoDaddy control panel)

Where it redirects WITH the !^staging lines in the htaccess:

https://www.___.com

Am I missing something obvious?


Solution

  • This answer probably won't help future people who stumble upon this page, but thought I should answer anyways:

    There was a lot of other miscellaneous crap in the htaccess file; the site has been around for many years and I recently "inherited" it as part of my job duties. After removing all of those lines, the staging redirect works now. I still can't see what exactly it was that would cause an issue, but in any case, it works, and hopefully we don't need any of those other old rules. (AFAIK we don't.)