Redirecting “/” Root URL to Another Sub-Directory or URL

A typical redirect is usually done for either:

  • The entire website to another website:
    Redirect /
  • Or from one specific path of a website to another location:
    Redirect /path

The limitation of Apache’s Redirect directive is that you cannot partially (nor selectively) redirect only the root URL “/”, without also redirecting everything after it to another path or location (e.g., redirect “/” will also match and redirect on request for “/folder/”)…

This is because Redirect matches everything after the given path, and a redirect from “/” is a redirection of all the website’s URLs.

Use RedirectMatch to only redirect the root URL “/” to another sub-directory or URL, without also redirecting everything else…

RedirectMatch ^/$

Since RedirectMatch uses a regex match, it can be specific with the “/” path without matching everything else.

22 thoughts on “Redirecting “/” Root URL to Another Sub-Directory or URL”

    1. What is main shop? And is this an external or internal redirect?

      To redirect all requests “internally” to the above URL, you would place something like this in the webroot’s .htaccess file –

      RewriteRule ^(.*)$ /base/app1/app2/Major.php/$1 [L,QSA]

      This way requests for "/some-url?q=example" will internally be sent to URL "/base/app1/app2/Major.php/some-url?q=example".

  1. I would like to know how would I redirect a sub-domain using the above redirect? to

    So when anyone tried to access they would automatically be redirected to instead!

    Redirect /one

    RedirectMatch ^/one/(.*)$$1

    Thanks in advance

    1. Either of your code examples should work (as long as mod_alias is loaded)…

      You can place either of the lines in the site’s .htaccess file, or the HTTP and SSL VirtualHost files.

  2. Hi,

    Thanks a lot for this trick !
    I wanted to redirect my root to an url and my regular pages to another and you helped me a lot !!

    To be more clear, I had :

    I wanted to be redirected to
    and* to be redirected to

    Thanks to you I could do it. Here’s the .htaccess in my

    RedirectMatch permanent ^/$
    Redirect permanent /

      1. Hello,

        thanks for the tip & it worked. Recently we migrated from Apache 2.2 to Apache 2.4 & we are using below to redirect / url to /home with below :
        RewriteCond %{HTTP_HOST}\?cc= .*name\.(?:com.|co.)*(..)+(\?cc=
        RewriteRule ^/$ [R=301,L]
        RewriteRule ^/$ %{REQUEST_SCHEME}/home [R=301,L]

        This didnt work; I tried adding below:

        RedirectMatch “^/$” “”

        This one too didnt work. Could you please help me on this.

  3. How do you use your example to redirect from just the directory that the .htaccess file is in to a relative directory. for example I would like to redirect from /public_html/folder1/ to /public_html/folder1/folder2/ and the .htaccess would be in folder1

    1. Redirect /public_html/folder1 /public_html/folder1/folder2/

      or maybe…

      Redirect 301 /public_html/folder1/folder2/

      I’m not sure if the 2nd example will work once the request reaches the further-down folder, as upper-folders’ .htaccess directives still get run. So it might get you into a redirect loop.

      Anything “relative” and non-specified will come with issues, and you’ll need to construct a much more involved RewriteCond and RewriteRule regex.

      1. Yeah, those give me an infinite redirect. Here is what I came up with using mod_rewrite

        RewriteEngine On
        RewriteCond %{REQUEST_URI} !^/public$
        RewriteRule ^(.*)/$ /public/ [R=301,L]

        where the htaccess exists in the directory just above public. The problem is that we have no control over where the app is installed. I’m wanting to make sure I have a solution that can be installed in the {documentroot} or in {documentroot}/folder1/. I think this should do the trick, but I’m open to thoughts.

  4. Nice !!!

    RedirectMatch …

    Great tip !!!!

    Reminder – the above can be placed inside the vhost definition

  5. Many thanks, that nicely solved a Google Search Engine “non canonical” issue considering “/” and “/index.php” as a duplicate content -_-‘

    I saw on doc’s samples that arguments are defined between double quote (“regex” not regex).
    So I’ve done it.


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>