How to redirect root and only root via htaccess?
123,991
Solution 1
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.
Solution 2
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.
Solution 3
This should work just fine:
RedirectMatch 301 ^/$ https://example.com/
Related videos on Youtube
Author by
Admin
Updated on September 17, 2022Comments
-
Admin over 1 year
I want to redirect only my root to another url, but maintain all the /sub/directories where they belong (and redirect)
example:
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
-
sharoz almost 11 yearsThis will NOT do what jardel wants. It will redirect ALL pages rather than just the root.
-
jaygooby almost 10 yearsIn 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 -
MrWhite about 7 yearsThis would be preferable (to the existing answers) if you are not already using mod_rewrite for other redirects.
-
MrWhite almost 6 yearsIt 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 theREQUEST_URI
server variable. -
CrandellWS almost 4 yearsmaybe a 302 would be a better