How do I write a rewrite-rule that redirects visitors to the domain www.mydomain.com/ to www.mydomain.com/index.html?
4 Answers
So you want to redirect nothing (^$) to index.html? That would then look like
RewriteRule ^$ index.html [L]
If you want to avoid both the / and /index.html being indexed by search bots, then add R=301 to make it a permanent redirect rather than a temporary redirect (302, which is the default). This would let the bots only index the /index.html.
RewriteRule ^$ index.html [R=301,L]
- 1,082,665
- 372
- 3,610
- 3,555
-
are there any effects on SEO by adding R=301 ?..... well it stop search engines to index the index.html page? if it does then it should not be added, – Zia Khan Feb 23 '23 at 19:38
-
This is already explained in the answer. – BalusC Feb 23 '23 at 19:45
What BalusC said - but consider whether you really want to redirect them. Wouldn't it be better to just serve index.html when the browser requests /, like most servers do? It's an extra round-trip to the server for no gain and just makes the URL longer. It's so 1990s. :)
- 59,148
- 53
- 164
- 220
-
2Seeing the context of the question (apache already does that by default) I rather think he just want to get rid of it :) – BalusC Dec 16 '09 at 00:22
-
I wanted it for this reason: http://serverfault.com/questions/94706/how-to-password-protect-the-domain-with-htaccess – Robin Rodricks Dec 16 '09 at 13:28
One way is to put your index.html in another folder, for exemple: domain.com/welcome/index.html and do a R301 from your CPanel. It's a wordaround but it worked for me. Have the same issue.
- 11
- 2
Is it possible there's a mistake above? It didn't work for me, redirecting me to a very long filepath ending in /index.html
The code that worked for me is:
# These two lines redirect the root to index.html.
RewriteRule ^$ /index.html [R=301,L]
RewriteRule ^/$ /index.html [R=301,L]
I found this solution at .htaccess redirect root to index.php
- 17
- 1
- 6