How to get Apache2 to redirect to a subdirectory

175,803

Solution 1

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.

This code could do in a .htaccess file (there is a tag for this, so I assume that is the original use case). But if you can edit ,the main server apache config then put it in the section for your website probably inside a <VirtualHost> section.

The docs for RedirectMatch say that the context can be "server config, virtual host, directory, .htaccess".

Solution 2

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.

Solution 3

You can use Redirect directive.

<Directory />
   Redirect permanent / http://git.example.com/git/
   ...
</Directory>
Share:
175,803

Related videos on Youtube

Hamish Downer
Author by

Hamish Downer

Unless I explicitly state otherwise in a post, all my original contributions to StackOverflow and ServerFault are placed into the public domain. If this is not legally possible, then anyone receiving a copy of them by any means is granted a non-exclusive perpetual license to use, distribute and modify them, and to distribute modifications, under any license or none, with or without attribution to me. Please note that this license applies only to my original contributions.

Updated on September 17, 2022

Comments

  • Hamish Downer
    Hamish Downer over 1 year

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

    • WerkkreW
      WerkkreW almost 15 years
      There are many ways you could approach this, but what is exactly what you are trying to accomplish?
  • Davor Josipovic
    Davor Josipovic about 9 years
    Indeed. Redirect / /git/ results in recursive fireworks.
  • Jonah Benton
    Jonah Benton over 7 years
    ErrorDocument should return an error page, not a useful document. This is important for crawlers.
  • user230910
    user230910 over 7 years
    ok, so where to put this line of code?
  • Mat M
    Mat M over 6 years
    I had to put a full URI (https://example.com/git/) for this to work. Strange.
  • 把友情留在无盐
    把友情留在无盐 over 4 years
    This NOT work! Firstly, <Directory ...> tags are used for local filesystem path, not for url. For url use <Location ...> tags. Secondly, "Redirect" matches partial url from the left. Thus /git is also a match, creating a infinite redirect loop.
  • Kiril
    Kiril about 3 years
    You have to use double quotes as per documentation you've linked.