Anyone out there speak mod_rewrite?
I have a bunch of subdomains I want to redirect on arrival, all with the same patterns. So, for example, trac.acidlabs.org would get redirected to www.acidlabs.org/trac.
Simple. Easy. I’m sure, but I don’t speak fluent mod_rewrite.
Anyone?


Take a look at the following forum post. I think it has all the information you need… http://forum.modrewrite.com/viewtopic.php?t=2149&sid=a03dbb5eb05348be32042234a656210b
This one should do it:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.] ).example.com$
RewriteRule ^$ http://www.example.com/%1/ [R,L]
Just change the example.com there to your domain.
Right now it will match only requests coming to http://trac.example.com/, but not http://trac.example.com/something/else/. If you want to pass on the path too then use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.] ).example.com$
RewriteRule (.*) http://www.example.com/%1/1 [R,L]
Are you sure you want to mod_rewrite? Redirecting with a .htaccess in your subdomain would do the trick nicely:
Redirect permanent / http://www.acidlabs.org/trac
Oh, if you vhost where you’ll use this rewrite block serves that same www site as well, then you need another condition to exclude www domain.
Something like:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTP_HOST} ^([^.] ).example.com$
RewriteRule ^$ http://www.example.com/%1/ [R,L]
[quote comment=”9830”]Are you sure you want to mod_rewrite? Redirecting with a .htaccess in your subdomain would do the trick nicely:
Redirect permanent / http://www.acidlabs.org/trac/quoteNope. None of the subdomains exist physically and I?m on a shared host where I don?t get to play that game. So, I want the subdomains to end up at a physical subdirectory. Not ideal, but good enough.
[quote comment=”9831”]Oh, if you vhost where you’ll use this rewrite block serves that same www site as well, then you need another condition to exclude www domain.[/quote]@Erki - my friend, that gives me 500 errors… Any ideas?
What version of Apache are you using? mod_rewrite differs between 1.3, 2.0 and 2.2 a bit. Also do you have access to error log, maybe something is listed there that shows what’s wrong. I ran this same rewrite rule in windows Apache 2.0 (not in .htaccess file, instead in vhost config) and it worked as expected.