Join 3,434 readers in helping fund MetaFilter (Hide)


Redirecting subdomains with mod_rewrite
August 19, 2005 4:01 PM   Subscribe

A subdomain redirection question (any mod_rewrite experts?)

So let's say that I'm setting up my new site, sports.com. Here is what I'd like to do:

When someone visits http://baseball.sports.com, I would like them to be redirected to http://sports.com/index.php?id=baseball (but transparently, so http://baseball.sports.com still appears in their browser). I would like to do this with several subdomains, but I don't need to handle *.sports.com (my webhost doesn't support wildcarded DNS entries anyway).

I realize I could fake this with frames, but that seems kind of lame. After wading through tons of .htaccess & mod_rewrite info, I'm more confused than ever. Can anyone help me pull this off?
posted by subclub to Computers & Internet (12 answers total)
 
No time to test it, but point all the domains at one server and something like

RewriteCond %{HTTP_HOST} ([^.]+)\.sports.com [NC]
RewriteRule /index.php?id=%1

should work.
posted by nicwolff at 4:37 PM on August 19, 2005


Oooho!! Could I pretty-please tack on a similar question?

How do I redirect into a subdirectory without the .htaccess rules filtering down into said subdirectory from the parent file?

As in, visiting www.domain.com should automatically forward to www.domain.com/photos

So, in ~/public_html/.htaccess:
Redirect / http://www.domain.com/photos

Great! That works, but then when the browser arrives at ~/public_html/photos/, it looks for any parent .htaccess rules, finds one in the directory above, and does the redirect a second time, so then the browser goes forward again to www.domain.com/photos/photos, then fails.

What gives?
posted by odinsdream at 4:43 PM on August 19, 2005


nicwolff:

using that code a get a 500 error. i think the first line should be:
RewriteCond %{HTTP_HOST} ([^.]+)/.sports.com [NC]

when i do that, there are no errors, but it doesn't actually do any redirecting (just dispalys whatever is actually at baseball.sports.com).
posted by subclub at 5:08 PM on August 19, 2005


wow, my typing is horrible.
posted by subclub at 5:15 PM on August 19, 2005


i should also note that with this code:

RewriteCond %{HTTP_HOST} ([^.]+)/.sports.com [NC]

i still get a 500 error on plain old sports.com
posted by subclub at 5:35 PM on August 19, 2005


odinsdream:

RewriteEngine On
RewriteCond %{REQUEST_URI} !photos [NC]
RewriteRule ^(.*) http://www.domain.com/photos



Untested.
posted by null terminated at 5:46 PM on August 19, 2005


"using that code a get a 500 error. i think the first line should be:
RewriteCond %{HTTP_HOST} ([^.]+)/.sports.com [NC]"

No. '\.' means a dot ('\' = 'the next character is the real one') since a dot on it's own means any character. '/.' means just slash dot - so unless your domain really is 'baseball/.sports.com' it wouldn't match the pattern and not do anything.

I'm not sure why the original suggestions 500s - I think it's because [^.]+ would be a test for "not anything more than once". I would have used

RewriteCond %{HTTP_HOST} (.+?)\.sports\.com [NC]

But then I usually never get regexps right on the first go. It mignt need the 'start of line' marker at the front.

RewriteCond %{HTTP_HOST} ^(.+?)\.sports\.com [NC]

(all are untested. ymmv.)
posted by Auz at 5:59 PM on August 19, 2005


The RewriteRule line is causing the syntax error; it's missing the replaced pattern and only includes the substitution.

However, it still doesn't quite work; the problem is that you want to change hostnames, and an internal rewrite can't do that. You need to do an internal proxy request using the [P] flag on the RewriteRule line:
RewriteCond %{HTTP_HOST} ([^.]+)\.sports.com [NC]
RewriteRule ^(.*) http://sports.com/index.php?id=%1 [P]
The rule I gave makes any request to any baseball.sports.com return the same index page, but that's probably not what you want. Unfortunately, I'd need to know a bit more about how your web app is structured before I could do better. For example, given your example just references a generic index page, maybe you just want the subdomain removed from the domain name and added generally as a query. For that, the 2nd line would be RewriteRule (.*) http://sports.com$1?id=%1 [P] . All in all, I'd recommend just doing a browser redirect, since it's well understood by users, and prevents you from having to think too hard about what the various permutations of subdomain URLs.

PS. odinsdream, just use RedirectMatch ^/$ http://www.domain.com/photos so only the top level URL and no others are redirected.
posted by boaz at 6:43 PM on August 19, 2005


Oh yeah, and I really shouldn't have just copied nicwolff's RewriteCond line, since it's got a few unescaped periods. Howzabout RewriteCond %{HTTP_HOST} ([^\.]+)\.sports\.com [NC] instead?
posted by boaz at 7:11 PM on August 19, 2005


boaz: Thanks, that works perfectly!
posted by odinsdream at 9:49 PM on August 19, 2005


It should be unnecessary (but harmless) to backslash . inside of braces. [.] means a character class consisting of a period, not of any character.
posted by Rhomboid at 1:51 AM on August 20, 2005


The RewriteRule line is causing the syntax error; it's missing the replaced pattern and only includes the substitution.

Whoops! Thanks for fixing that...
posted by nicwolff at 8:11 PM on August 20, 2005


« Older Samba and the Mac OS X Finder ...   |  I have to do a new XP install.... Newer »
This thread is closed to new comments.