I need to redirect from http://example.com/
to http://example.com/index.php
.
Asked
Active
Viewed 1.9k times
4 Answers
22
Use this:
DirectoryIndex index.php

Uwe Keim
- 39,551
- 56
- 175
- 291

cfedermann
- 3,286
- 19
- 24
-
1For SEO reasons, it is _not_ ok to have the same content for two different URLs. So `DirectoryIndex` does _not_ help for the SEO scenario. – Uwe Keim Feb 14 '18 at 13:20
4
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^$
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^$ http://example.com/index.php [L,R=301]
-
Why do you need `mod_rewrite` for this ? `DirectoryIndex` is the correct answer here. – Pierre-Olivier Apr 16 '12 at 14:36
-
there's a difference in the way both work. default directory does not change the uri hence if you need to make some manipulations on it this is a mess. – Sebas Dec 08 '12 at 23:29
-
For SEO reasons, it is _not_ ok to have the same content for two different URLs. So `DirectoryIndex` does _not_ help for the SEO scenario. – Uwe Keim Feb 14 '18 at 13:20
-
this solution is more flexible, since directory index cannot redirect as OP requested – pc_ Jan 12 '19 at 11:30
2
Just to add my own solution, since the answer of Michael did not work for me, I did something like:
RewriteEngine on
# These two lines redirect non-www to www.
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) https://www.example.com$1 [R=301,L]
# These two lines redirect the root to index.html.
RewriteRule ^$ /index.html [R=301,L]
RewriteRule ^/$ /index.html [R=301,L]
This also preserves any possible existing query string parameters.

Uwe Keim
- 39,551
- 56
- 175
- 291
-
1for me this line helped thank you `RewriteRule ^/$ /index.html [R=301,L]` – vikas etagi Mar 10 '23 at 06:48
1
Adding to @Uwe Keim's answer above, I had non-www to www setup on Apache virtual hosts.
What worked for me on .htaccess was:
RewriteEngine on
# Two lines to redirect from the root web directory to myfile.html.
RewriteRule ^$ /index.html [R=301,L]
RewriteRule ^/$ /index.html [R=301,L]

Moses J
- 91
- 1
- 5