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?
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.
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
.
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 .
Hi,
Well, I have tried the methods mentioned above! it's working yes, but not exactly the way I wanted. I wanted to redirect the default page extension to the main domain with our further action.
Here how I do that...
# Accesible Index Page
<IfModule dir_module>
DirectoryIndex index.php index.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(html|htm|php|php3|php5|shtml|phtml) [NC]
RewriteRule ^index\.html|htm|php|php3|php5|shtml|phtml$ / [R=301,L]
</IfModule>
The above code simply captures any index.* and redirect it to the main domain.
Thank you
index.html
andindex.php
exist, or just one? – nkorthindex.html
inDirectoryIndex
? Then it would be the default forexample.com
. The other file should be available atexample.com/index.php
. – bfavarettoindex.php
and make sure it is not redirecting you to/
using someheader
function. – anubhava