0

I have created a subdomain in my GoDaddy cPanel and want to work on it as a dev site. In my htaccess, which is within the public_html directory, I have some code to do two things: redirect www to non-www and redirect http to https:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
# END Force SSL

# BEGIN Force HOSTNAME

RewriteEngine on

RewriteCond %{HTTP_HOST} !^example.com [NC]

RewriteRule ^(.*)$ https://example.com/$1 [L,R=301,NC]

# END Force HOSTNAME

The problem is that it redirects my subdomain, which should be subdomain.example.com to example.com/subdomain/

How do I continue to redirect http to https and www to non-www on my main domain, but disable this redirect on the subdomain? It would be nice to keep https on the subdomain, but is not needed.

If I remove the force hostname code, the subdomain works (as such: subdomain.example.com), but no longer redirects www to non-www.

ssmyth
  • 1
  • 1
  • 1) You only need to specify `RewriteEngine On` once; 2) I think you intended the `Force HOSTNAME` stuff to redirect www to non-www, but actually it redirects *any* hostname to the non-hostname. If you want instead to leave your other hostnames alone (like `subdomain`), you need to explicitly restrict the rule to match `www`. Try `RewriteCond %{HTTP_HOST} ^www\. [NC]`. There are [many duplicates](https://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www) of doing this on SO, check them out if you're still having trouble. 3) Use `R=301` for your https redir too. – Don't Panic Dec 06 '19 at 08:55
  • Thanks, and I realize that there are many answers for www to non-www - I have been over many of them, but none seem to answer my problem. With your example, I'm still getting the same results. Even with my code, test.example.com does not redirect to example.com, but subdomain.example.com redirects to example.com/subdomain. The only way I can seem to fix this is by moving the directory for that subdomain outside of public_html and into the root directory, which doesn't seem right. I'm guessing this is because it doesn't get to the htaccess file which is in the public_html directory @Don'tPanic – ssmyth Dec 06 '19 at 19:05
  • Sounds like your browser is caching redirects. Every time you change your htaccess, clear browser cache, quit and restart. Double check there are no other .htaccess files floating around. What you are trying to do is very simple and standard, no need to move directories or look for complicated rules. 4 lines and you're done. – Don't Panic Dec 06 '19 at 19:10

1 Answers1

0

Needed to add an additional RewriteCond to the force SSL section:

RewriteCond %{HTTP_HOST} ^(.)?example\.com
tripleee
  • 175,061
  • 34
  • 275
  • 318
ssmyth
  • 1
  • 1