Thursday, January 13, 2011

How to get apache2 to redirect to a subdirectory

I am running apache2 on debian etch, with multiple virtual hosts.

I want to redirect so that http://git.example.com goes to http://git.example.com/git/

Should be really simple, but google isn't quite cutting it. I've tried the Redirect and Rewrite stuff and they don't quite seem to do what I want ...

  • Feel a bit silly - a bit more googling turned up the answer I was after:

    RedirectMatch ^/$ /git/
    

    Basically redirecting the root, and only the root.

  • On my own website, I used a 301 redirect by placing the following in index.php in the root directory:

    <?php
    // Permanent redirection
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://git.example.com/git/");
    exit();
    ?>
    
    David Zaslavsky : That works, but using PHP just to do a redirect is overkill (and not as efficient as Apache directives).
  • You've got the correct answer there with the redirect. You have to be careful when redirecting everything to somewhere else, since you can get recursive redirects there. This happens if you want to put up a maintenance page.

  • You can use Redirect directive.

    <Directory />
       Redirect permanent / http://git.example.com/git/
       ...
    </Directory>
    

0 comments:

Post a Comment