mod_rewrite help needed


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?

7 Replies to “mod_rewrite help needed”

  1. 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]

  2. [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%5B/quote%5DNope. 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.

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

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

Leave a Reply