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

  • There are many ways you could approach this, but what is exactly what you are trying to accomplish?
    – WerkkreW
    May 19, 2009 at 17:24

4 Answers 4


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

  • 3
    ok, so where to put this line of code?
    – user230910
    Jan 10, 2017 at 5:41
  • 2
    I had to put a full URI (https://example.com/git/) for this to work. Strange.
    – Mat M
    Sep 22, 2017 at 14:09
  • You have to use double quotes as per documentation you've linked.
    – Kiril
    Apr 23, 2021 at 15:08

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.

  • 7
    Indeed. Redirect / /git/ results in recursive fireworks. May 9, 2015 at 20:51

You can use Redirect directive.

<Directory />
   Redirect permanent / http://git.example.com/git/
  • 2
    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. Dec 12, 2019 at 13:04

The accepted answer resolved my issue, but I also found that I had to add a 404 redirect for non-existant pages -- my situation is that have an OwnCloud installation located one level below root (https://example.com/owncloud).

This worked for me, to send everything to my subdirectory:

# redirect from root to subdirectory
RedirectMatch ^/$ /thesubdirectory/

# redirect on 404 to subdirectory
ErrorDocument 404 /thesubdirectory/index.php
  • 3
    ErrorDocument should return an error page, not a useful document. This is important for crawlers. Sep 11, 2016 at 22:01
  • You should not be redirecting 404 missing pages unless they are substantially related to the page(s) you are redirecting to (i.e. significant content shared between them) - in which case the original redirect statement(s) from the previously existing pages should be doing the job. If they are simply pages that no longer exist, rather than being moved, then Google and your visitors should be told they no longer exist by means of a 404 page. The Google index will be properly updated and visitors/webmasters will update their bookmarks and links. Oct 23, 2023 at 11:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .