Create index.html by default, but allow index.php to visit if typed

I have the following line in my .htaccess file:

DirectoryIndex index.html index.php 

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

+48
html .htaccess
Apr 03 2018-12-12T00:
source share
5 answers

By default, 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 search each of the above files in order and serve the first one that it finds when the visitor requests only the directory. If the web server does not find files in the current directory that match the names in the DirectoryIndex directive, the browser will display a list of directories showing all the files in the current directory.

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

Link: Here .

+72
Apr 10 '12 at 3:23
source share
— -

If you are using WordPress, now there is a problem with the filter:

 remove_filter('template_redirect', 'redirect_canonical'); 

(Put this in your functions.php theme)

This tells WordPress not to redirect index.php back to the root page, but to sit where it is. Thus, index.html can be set as the default page in .htaccess and can work next to index.php .

+11
Jun 17 '15 at 19:57
source share

I agree with @TheAlpha's accepted answer, Apache reads the target DirectoryIndex files from left to right, if the first file exists, apche serves it, and if it does not, the next file will serve as an index for the directory. Therefore, if you have the following directive:

 DirectoryIndex file1.html file2.html 

Apache will serve /file.html as an index, you will need to reorder the files if you want to set / file 2.html as an index

 DirectoryIndex file2.html file1.html 

You can also set the index file using RewriteRule

 RewriteEngine on RewriteRule ^$ /index.html [L] 

RewriteRule above will rewrite your homepage in /index.html, the rewriting takes place inside, so http://example.com/ will show you the contents of index.html,

+3
Feb 25 '17 at 9:35
source share
 RewriteEngine on RewriteRule ^(.*)\.html$ $1.php%{QUERY_STRING} [L] 

Place these two lines at the top of your .htaccess file. It will display .html in the url for your .php pages.

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

Use this to display .php in the url for your .html pages.

+1
Apr 10 2018-12-12T00:
source share

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

does he have any means? you may just need to add like this!

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

enter image description here

+1
01 Oct '16 at 12:56
source share



All Articles