1

I created a subdomain in cPanel and set it to redirect to the www version of my site. The redirecting works insofar as redirecting subdomain.example.com to www.example.com.

But when a visitor tries to go to subdomain.example.com/file1, he is taken to a 404 page instead of www.example.com/file1.

How do I fix this?

Edit: This is the .htaccess that cPanel has created in the subdomain's directory (which btw is just public_html\subdomain

RewriteEngine on

RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.subdomain\.example\.com$
RewriteRule ^/?$ "http\:\/\/www\.example\.com\/" [R=301,L]
  • 2
    Yeah. That code is horribly wrong. The escapes (slash) in the RewriteRule is wrong as well as the quotes. This is the second I have seen here this weekend. I am surprised that cPanel writes such poor code. – closetnoc Apr 12 '15 at 1:44
1

Turn off the cPanel redirect. Not sure what it is doing or how. But clearly it is not working right.

Create an .htaccess file in your sub-domain root directory with:

RewriteEngine on
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

...in it. Obviously change example.com to your domain name. That should do what you want.

If you do not know where that directory is, cPanel should be able to tell you. I do not use cPanel so I could not walk you through this. Sorry. But if you cannot find it and have shell access to your server, you can look at /etc/apache2/sites-available/. Your configuration file should be obvious. You can get the directory location from that. If sites-available is not there, then look at /etc/apache2/ for apache2.conf or httpd.conf. It should be defined there. Different Apache installs either use the new format (sites-available) for virtual hosting or the old format for single site hosting which can also be used for virtual hosting with more work. For example Ubuntu uses the new format and Redhat uses the old.

  • In .htaccess you will need an additional slash in the substitution. ie. example.com/$1. – MrWhite Apr 11 '15 at 17:29
  • @w3d Good catch!! As much as I do this you think these simple errors would get caught. I am glad you came along!! What I would I do without you?? Thanks!! I consider you top-dog on .htaccess and regex around here. There is no wonder on why. – closetnoc Apr 11 '15 at 17:34
  • Well, if this was in your server config then you might not need the slash - so it's an easy mistake. Thanks for the kind words; still very much learning this stuff myself! :) – MrWhite Apr 11 '15 at 18:21
  • @w3d I don't see the additional slash in your comment. I am editing the question to include the htaccess that cPanel has created. – Knocks X Apr 12 '15 at 1:16
  • @KnocksX I already corrected the answer to reflect the change. w3d is the best here for this stuff. I trust him without question. – closetnoc Apr 12 '15 at 1:41

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.