I want to redirect only my root to another url, but maintain all the /sub/directories where they belong (and redirect)


mysite.com/1 redirects to somewhere mysite.com/admin opens a page

i want mysite.com/ to redirect to mysecondsite.com and only this with a 301 redirect using htaccess

4 Answers 4


Try this:

RewriteEngine on
RewriteCond %{HTTP_HOST} mysite\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://mysecondsite.com/ [L,R=301]

If you don't need to check for the old domain (for example, if the directory where your .htaccess is placed is only used by the old domain) you can remove the second line.

  • 1
    In my case, I needed to redirect traffic to mysite.com/ but not foo.mysite.com so had to add a regex to line 2: RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC] to ensure the match was for the exact host
    – jaygooby
    Jun 23, 2014 at 13:18
  • It is more efficient to check the URL-path in the RewriteRule pattern (as in @DaveForgac's answer), instead of using an additional condition to check the REQUEST_URI server variable.
    – MrWhite
    Jul 29, 2018 at 17:38
  • This will also redirect root with parameters: this.com/?a=123
    – Ralf
    Oct 27 at 12:32
  • To exclude URLs with certain parameters, add: RewriteCond %{QUERY_STRING} !my_parameter= stackoverflow.com/questions/12134384/…
    – Ralf
    Oct 27 at 12:45

If you mean you'd only like to redirect "/" to another domain, this will work:

RewriteEngine on
RewriteRule ^$ http://www.example.com/ [R=301,L]

This only matches the domain's root with nothing after it so it will only redirect the domain name without a filename specified.

  • A slight variation worked for me. Added to the bottom of my htaccess, after all other rules invoked, I just wanted to render a welcome page to anyone visiting my root domain (without any subdomain): RewriteCond %{HTTP_HOST} !^(subdomain1|subdomain2)\.mysite\.com$ [NC] RewriteRule ^$ /welcome.php [NC,L]
    – Kalnode
    Jul 12, 2018 at 0:12
  • This will also redirect root with parameters: this.com/?a=123
    – Ralf
    Oct 27 at 12:33

This should work just fine:

RedirectMatch 301 ^/$ https://example.com/
  • 2
    This would be preferable (to the existing answers) if you are not already using mod_rewrite for other redirects.
    – MrWhite
    Mar 14, 2017 at 22:25
  • maybe a 302 would be a better
    – CrandellWS
    Jul 14, 2020 at 13:51
  • This will also redirect root with parameters: this.com/?a=123
    – Ralf
    Oct 27 at 12:31

A shorter solution:

Redirect 301 / http://mysecondsite.com/
  • 14
    This will NOT do what jardel wants. It will redirect ALL pages rather than just the root.
    – sharoz
    Jul 16, 2013 at 23:49

You must log in to answer this question.