Davinel
Davinel

Reputation: 960

.htaccess redirect root to index.php

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

Upvotes: 12

Views: 19396

Answers (4)

Moses J
Moses J

Reputation: 111

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]

Upvotes: 1

cfedermann
cfedermann

Reputation: 3284

Use this:

DirectoryIndex index.php

Upvotes: 21

Michael
Michael

Reputation: 499

Try this:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^$
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^$ http://example.com/index.php [L,R=301]

Upvotes: 5

Uwe Keim
Uwe Keim

Reputation: 40726

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.

Upvotes: 2

Related Questions