Apache: How to change the default index page
The below command will set the default page your website shows to whatever you specify. For example the below command will set your website to show the index.html file as default.
DirectoryIndex index.html |
The below change will attempt to show business.html as the default page for your website and every directory.
DirectoryIndex business.html |
And you can even do something cool like have it load a movie or image by default.
DirectoryIndex cool.mov |
Or even specify multiple files so that it will try to load the first one, then the next, and so on. So with the below line it would try to load business.html, then if that wasn’t there index.cgi, and if that wasn’t there index.pl, and so on.
DirectoryIndex business.html index.cgi index.pl default.htm |
If it can’t find the file you specify it will revert to a directory listing of the files in that folder.
Ethel Grace November 20, 2013 at 8:59 am
Hi. Mine still isnt working. Still displays the cgi webpage.
James Davey November 20, 2013 at 9:34 am
Hello Ethel,
I am sorry to hear that. Your browser may be loading a cached version of the page – try clearing your browser cache (http://kb.site5.com/browsers/how-to-clear-the-browser-cache/) and try loading the site again.
Timothy February 11, 2014 at 8:43 pm
Works great
Lontarnet March 1, 2014 at 12:55 am
Thanks.
When I access mywebsite.com, I will be redirected to mywebsite.com/custom-page.php
but, how to do this?
when I access mywebsite.com/index.php, I will not be redirected to mywebsite.com/custom-page.php
James Davey March 3, 2014 at 8:02 am
Hi there,
When you visit mywebsite.com/index.php you’re specifying a file, so the DirectoryIndex will be ignored. If you want this to redirect to another page, you would need to use the steps at http://kb.site5.com/control/siteadmin/siteadmin-url-redirect/
Mark January 9, 2015 at 1:10 pm
It’s a simple redirect, I don’t know why James didn’t just tell you over trying to gather web traffic.
Your .htaccess should contain this to redirect:
RewriteEngine On
RewriteRule ^/?$ /custom-page.php [L,R=301]
It’s the “R=301” that does the redirect.
Hope this helps.
Teo February 7, 2015 at 9:58 pm
Can someone give me the exact and entire code to place on the htaccess file?
I need to redirect to index.html …
And sometimes is redirecting to index.php of the wordpress site.
I need people to enter the html file instead.
Thanks
TEO
James Davey February 9, 2015 at 6:09 am
Hi Teo,
To direct traffic to index.html, you would add the following to your .htaccess file (preferably in the first line):
DirectoryIndex index.html
breack February 28, 2015 at 12:45 pm
Hi!
Look, I have a problem with my website. I have 0 experience in this, is my first website ever.
My website has 3 folders A,B,C and outside of them an empty file index.htm.
The default homepage is “index.htm”. But when the site is loaded the address is http://www.mysite.com and there is no index.htm extension.
Now…I want to change this default hompage with /A/ccc.html.
What is the code for this? I have tried everything and didn’t work….
Many thanks!
James Davey March 2, 2015 at 6:02 am
Hi there,
If you want your default page to be /A/ccc.html, you would need this code in your .htaccess file:
DirectoryIndex /A/ccc.html
hyip March 4, 2015 at 10:54 am
There are several method you can use:
RedirectMatch ^/$ /A/
or
RewriteEngine on
RewriteRule ^(.*)$ /ccc/$1 [L]
See more details here
http://stackoverflow.com/questions/990392/htaccess-rewrite-to-redirect-root-url-to-subdirectory
James Davey March 4, 2015 at 1:38 pm
These would also work, yes. Thank you for adding these :)
Vitoria Lemos May 7, 2015 at 5:16 pm
Hi there ^-^
Thank you for the article.
I’m still in trouble with my website’s homepage.
When I load it, the first page that appears is called Default.aspx, but I already have an index.html in the same folder and I also wrote this line (DirectoryIndex index.html) in the .htaccess file :/ so, what else can I do?
Thank you :)
James Davey May 8, 2015 at 5:24 am
Hello Vitoria,
The DirectoryIndex should load index.html by default. If there are redirects coded in that file, however, they would be applied after .htaccess.
Abhishek March 29, 2016 at 5:33 am
Hello James,
Thanks for this article. It helped me a lot. However, I am facing an issue here. Code DirectoryIndex index.html works fine for the root directory, but I do not want to it to change the default page for other folders. How do I do this?
Thanks.
James Davey March 29, 2016 at 6:14 am
Hello Abhishek,
Yes, simply place a .htaccess in each of the subfolders. You can set the default index file in each of these, but leaving each of these blank should be fine as well.
Abhishek March 29, 2016 at 6:29 am
Hello James,
Thanks for reply.
Well, I am using the same .htaccess file for each folder which is placed in root. So, it would not be possible. So, I am now using the following code for this.
RewriteEngine On
RewriteRule ^/?$ home.php [L]
Link is http://stackoverflow.com/q/36280634/4932070
James Davey March 29, 2016 at 9:31 am
That would work, yes. If you have copied the same .htaccess file to each folder, but do not want that DirectoryIndex line, you can simply remove that line from the subfolder .htaccess files.