Make index.html default, but allow index.php to be visited if typed in

Go To StackoverFlow.com

56

I have the following line in my .htaccess file:

DirectoryIndex index.html index.php

Everytime I go to index.php it takes me to index.html. Is it possible to allow for both, but leave index.html the default for users visiting www.domain.com?

2012-04-03 22:35
by Matt Rowles
Do both index.html and index.php exist, or just one - nkorth 2012-04-04 03:14
They both exist. Are you saying my .htaccess file should allow me to visit both - Matt Rowles 2012-04-04 06:22
Shouln't you have only index.html in DirectoryIndex? Then it would be the default for example.com. The other file should be available at example.com/index.php - bfavaretto 2012-04-10 03:21
It actually redirects you to index.html even though index.php exists? There some other configuration interfering then. Perhaps a rewrite rule gone awry or something - Corbin 2012-04-10 03:25
I will suggest you to look into your index.php and make sure it is not redirecting you to / using some header function - anubhava 2012-04-11 13:48
I think it might be, it's a Wordpress install afterall - Matt Rowles 2012-04-12 03:00


87

By default, the DirectoryIndex is set to:

DirectoryIndex index.html index.htm default.htm index.php index.php3 index.phtml index.php5 index.shtml mwindex.phtml

Apache will look for each of the above files, in order, and serve the first one it finds when a visitor requests just a directory. If the webserver finds no files in the current directory that match names in the DirectoryIndex directive, then a directory listing will be displayed to the browser, showing all files in the current directory.

The order should be DirectoryIndex index.html index.php // default is index.html

Reference: Here.

2012-04-10 03:23
by The Alpha
Thanks mate, I'll remove the .htaccess file tonight and see if this fixes my issue. I thought it seemed a bit odd as it's never happened to any of my websites before this - Matt Rowles 2012-04-10 04:58
You are most welcome :- - The Alpha 2012-04-10 05:01


15

If you're using WordPress, there is now a filter hook to resolve this:

remove_filter('template_redirect', 'redirect_canonical'); 

(Put this in your theme's functions.php)

This tells WordPress to not redirect index.php back to the root page, but to sit where it is. That way, index.html can be assigned to be the default page in .htaccess and can work alongside index.php.

2015-06-17 19:57
by Natacha Beaugeais
This is great, thanks for updat - Matt Rowles 2015-06-17 23:12
That's a life saver! - ASR 2016-11-18 06:25


5

I agree with @TheAlpha's accepted answer, Apache reads the DirectoryIndex target files from left to right , if the first file exists ,apche serves it and if it doesnt then the next file is served as an index for the directory. So if you have the following Directive :

DirectoryIndex file1.html file2.html

Apache will serve /file.html as index ,You will need to change the order of files if you want to set /file2.html as index

DirectoryIndex file2.html file1.html

You can also set index file using a RewriteRule

RewriteEngine on

RewriteRule ^$ /index.html [L]

RewriteRule above will rewrite your homepage to /index.html the rewriting happens internally so http://example.com/ would show you the contents ofindex.html .

2017-02-25 09:35
by starkeen


1

RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php%{QUERY_STRING} [L]

Put these two lines at the top of your .htaccess file. It will show .html in the URL for your .php pages.

RewriteEngine on
RewriteRule ^(.*)\.php$ $1.html%{QUERY_STRING} [L]

Use this for showing .php in URL for your .html pages.

2012-04-10 11:51
by NoName
This looks nifty but not quite what I was after I'm afraid - Matt Rowles 2012-04-10 23:12


1

DirectoryIndex index.html index.htm default.htm index.php index.php3 index.phtml index.php5 index.shtml mwindex.phtml

it doesn't has any means? you may be just need to add like this!

<IfModule dir_module>
    DirectoryIndex index.php index.html index.htm
</IfModule>

enter image description here

2016-10-01 12:56
by xgqfrms