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

A typical redirect is usually done for either:

  • The entire website to another website:
    Redirect / https://www.example.com/
  • Or from one specific path of a website to another location:
    Redirect /path http://www.example.com/some-other/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 ^/$ http://www.example.com/another/path

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

16 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?

    https://mysite.com/one to https://one.mysite.com

    So when anyone tried to access https://mysite.com/one they would automatically be redirected to https://one.mysite.com instead!

    Redirect /one https://one.mysite.com/

    RedirectMatch ^/one/(.*)$ https://one.mysite.com/$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 :
    http://www.site1.com
    news.site1.com
    http://www.site2.com

    I wanted http://www.site1.com/ to be redirected to http://www.site2.com
    and http://www.site1.com/* to be redirected to news.site1.com

    Thanks to you I could do it. Here’s the .htaccess in my http://www.site1.com

    RedirectMatch permanent ^/$ https://www.site2.com
    Redirect permanent / https://news.site1.com/

      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 ^/$ http://site.name.com/home%2%1 [R=301,L]
        RewriteRule ^/$ %{REQUEST_SCHEME}/home [R=301,L]

        This didnt work; I tried adding below:

        RedirectMatch “^/$” “http://site.name.com/home/”

        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.

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>