How do I redirect subfolder URLs to subdomain URLs?
March 18, 2009 11:37 AM   Subscribe

Apologies for asking a question that I know will be answered already on the web, somewhere, but I can't find it. I'm having trouble writing a .htaccess ReWrite rule that will forward requests for all folders within a specific sub-folder to the same folders at a subdomain - so http://www.mydomain.com/hello/foo/bar/ will forward to http://hello.mydomain.com/foo/bar/ Any advice very gratefully received.
posted by Cobbler to Computers & Internet (12 answers total) 2 users marked this as a favorite
 
It would help greatly if you could post what you have for a rule now.

Also, this might be something worth asking at Stack Overflow.

My thought is that this rule should do it if placed in .htaccess in [www.mydomain.com DocumentRoot]/hello:

RewriteRule $1 http://hello.mydomain.com/$1 [R,QSA]
posted by mkb at 11:42 AM on March 18, 2009


Response by poster: At the moment all I have is the very basic main folder rule working:

RewriteRule ^hello http://hello.mydomain.com [R=301,L]
posted by Cobbler at 11:50 AM on March 18, 2009


Oof, that rule I wrote down won't work; it doesn't even make sense. Your problem is that everything will redirect to the root of hello.mydomain.com, which you don't want.

Try:

RewriteRule ^hello/(.*)$ http://hello.mydomain.com/$1 [R=3-1,L,QSA]
posted by mkb at 12:00 PM on March 18, 2009


Response by poster: It looks like it should work, but gives me 500 errors, I'm afraid - that's both with my original lines in place and with them removed.
posted by Cobbler at 1:42 PM on March 18, 2009


Geez, you will have to excuse my horrible typing. That '3-1' should be '301'.
posted by mkb at 1:56 PM on March 18, 2009


Response by poster: Heh. I'd already spotted that and fixed it. Sorry.
posted by Cobbler at 2:07 PM on March 18, 2009


Maybe the "301" gets this working for you, but if not, I've been using the following .htaccess for years to redirect from subdomain1.example.com to subdomain2.example.com for years. The file lives in the root of the subdomain1, which for you would be www.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain1.example.com$
RewriteRule ^(.*)$ http://subdomain2.example.com/$1 [R=permanent,L]


Basically I shoot people to the (shorter) subdomain1 address, and no matter what they type it automagically turns into subdomain2.
posted by niles at 2:08 PM on March 18, 2009


Response by poster: "no matter what they type it automagically turns into subdomain2"

Thanks, but I don't think that's what I actually want - it's only people who visit the specific /hello/ folder and deeper that should be redirected.
posted by Cobbler at 2:14 PM on March 18, 2009


Response by poster: mkb: My mistake, your suggestion does work, but now requests for the folder without a trailing slash bring up 404s.

i.e. while http://www.mydomain.com/hello/ forwards OK, http://www.mydomain.com/hello doesn't.
posted by Cobbler at 3:10 PM on March 18, 2009


It looks like it should work, but gives me 500 errors, I'm afraid - that's both with my original lines in place and with them removed.

Wait... you mean even without Rewrite rules you're getting a 500 error? Can't help you there. Something's drastically wrong with your setup.

If you are only getting 500 errors when you add any rewrite rule, the make sure that FollowSymLinks is set for that directory.
posted by sbutler at 3:20 PM on March 18, 2009


Response by poster: No, I'm not getting the errors now - see the post above yours.
posted by Cobbler at 3:25 PM on March 18, 2009


Best answer: Thanks everyone for your help - I got all the various URLs working with two lines:

RewriteRule ^hello(.*)$ http://hello.mydomain.com [R=301,L]
RewriteRule ^hello/(.*)$ http://hello.mydomain.com/$1 [R=301,L]

posted by Cobbler at 3:37 PM on March 18, 2009


« Older Please tell me how not to melt in my car.   |   Pre-nup woes Newer »
This thread is closed to new comments.