How to get Apache2 to redirect to a subdirectory
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>
Related videos on Youtube
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, 2022Comments
-
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 tohttp://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 almost 15 yearsThere are many ways you could approach this, but what is exactly what you are trying to accomplish?
-
-
Davor Josipovic about 9 yearsIndeed.
Redirect / /git/
results in recursive fireworks. -
Jonah Benton over 7 yearsErrorDocument should return an error page, not a useful document. This is important for crawlers.
-
user230910 over 7 yearsok, so where to put this line of code?
-
Mat M over 6 yearsI had to put a full URI (
https://example.com/git/
) for this to work. Strange. -
把友情留在无盐 over 4 yearsThis 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 about 3 yearsYou have to use double quotes as per documentation you've linked.