Heres the situation: I have a couple of different web projects running on a single shared host. Project 'A' is accessed through a subdomain (subA.mydomain.com) while project 'B' is the main mydomain.com

Both A and B are symlinks inside the host's public_html folder:

ln -s /home/myhost/ProjectA ~/public_html/subA
ln -s /home/myhost/ProjectB ~/public_html/main

The subdomain (project A) works fine and the site is running on it. For 'B' which is the main domain, i have used .htaccess to point primary domain to a subfolder as written here. The domain website is also working fine.

The problem is when i do mydomain.com/subA, the subdomain website (project A) opens. I tried adding redirect rules to point mydomain.com/subA --> subA.mydomain.com like so:

RewriteCond %{HTTP_HOST} ^mydomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteRule ^subA\/?$ "http\:\/\/subA\.mydomain\.com\/" [R=301,L]

But the url still reads mydomain.com/subA and the website shown is project A (with images and static files missing obviously as its a different web project).

Why isn't the redirect working?


Unless you have a RewriteBase / somewhere, ^subA\/?$ isn't going to match anything. Mod_rewrite looks at the entire url, including the leading /. Try:

RewriteRule ^/subA/?$ http://subA.mydomain.com/ [R=301,L]

Also, you can loosen up on the escaping. It doesn't hurt, but it also doesn't help anything, and it makes it harder to read.

| improve this answer | |
  • correct, missed that, I have concentrated on the end, not on the beginning :) – Aleksandar Ivanisevic Nov 14 '09 at 11:23

It seems the following line worked (and remove the earlier regex-heavy redirection code):

RedirectMatch 301 /subA/ http://subA.mydomain.com/

Thanks all!

| improve this answer | |

request URI for mydomain.com/subA is /subA (note no trailing slash) which will not match

change your rewrite rule to

RewriteRule ^subA http://subA.mydomain.com/ [R=301,L]

| improve this answer | |
  • hmmm...its not working. If i type "mydomain.com/subA" or "mydomain.com/subA/", the resulting url is still "mydomain.com/subA/". And i dont have anything else in htaccess besides what i mentioned in the question. – fenderplayer Nov 13 '09 at 20:38
  • Does it matter if theres an index.php file in "subA" (i'm using frameworks), as in "/home/myhost/ProjectA/index.php"? – fenderplayer Nov 13 '09 at 20:42
  • Should be RewriteRule ^/subA subA.mydomain.com [R=301,L], see the answer below if it still doesnt work add RewriteLog /tmp/rewrite.log RewriteLogLevel 9 and post the contents of rewrite.log – Aleksandar Ivanisevic Nov 14 '09 at 11:25

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.