12

I need to redirect from http://example.com/ to http://example.com/index.php.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Davinel
  • 940
  • 4
  • 10
  • 20

4 Answers4

22

Use this:

DirectoryIndex index.php
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
cfedermann
  • 3,286
  • 19
  • 24
  • 1
    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
4

Try this:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^$
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^$ http://example.com/index.php [L,R=301]
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Michael
  • 497
  • 2
  • 10
  • 22
  • 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
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