mod_rewrite help needed

October 17, 2006

in asides


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?

Related posts

{ 7 comments… read them below or add one }

Steve Webster October 17, 2006 at 11:00 pm

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

Reply

Erki Esken October 17, 2006 at 11:00 pm

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]

Reply

Jan gorman October 17, 2006 at 11:02 pm

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

Reply

Erki Esken October 17, 2006 at 11:03 pm

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]

Reply

trib October 18, 2006 at 5:33 pm

[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.

Reply

trib October 19, 2006 at 2:50 pm

[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?

Reply

Erki Esken October 19, 2006 at 5:52 pm

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.

Reply

Leave a Comment

Additional comments powered by BackType

Previous post:

Next post: