How can I replace index.html by a different file in .htaccess - .htaccess

When I browse to http://www.example.com I want to be redirected to http://www.example.com/home.html,
When I browse to http://www.example.com/index.php I want to be redirected to http://www.example.com/index.php,
How can I do that?

Hy ,
Just add the following line in you .htaccess file
DirectoryIndex something.html
where something.html will be your index page
As an extra note - see this page from docs on how to manage your .htaccess file.

Take a look at the DirectoryIndex command:
http://httpd.apache.org/docs/current/mod/mod_dir.html#directoryindex
It allows to configure what files are tried when a directory is requested. It also allows to add several options to be tried. This allows flexibility whilst you can still keep control over the options.

When a URL specifying only a directory is requested
eg.: http://www.domain.com/directory/
Apache looks for file names by default (in the order they appear):
index.html
index.htm
home.html
welcome.html
The .htaccess directive DirectoryIndex can be used to override this behavior. In your .htaccess file, type the following line:
DirectoryIndex index.php index.html

Related

Options +Indexes of .htaccess not working

I have a website running on a server. It's just an index.html with a hello world text. Also, I have a folder named /vpn which contains various txt files and an index.html file.
If I try to access the URL domain/vpn, it shows me the content of index.html.
I just need to show the files inside the folder vpn when the user tries to access domain/vpn.
I created an .htaccess file with the next content in the root:
RewriteEngine on
<If "%{REQUEST_URI} == '/vpn/'">
DirectoryIndex disabled
Options +Indexes
</If>
When I try to access to vpn, it shows me a 404 error, the requested URL was not found on this server.
.htaccess is applying the DirectoryIndex rule (If a delete it, it shows me index.html content again), but not the Options +Indexes one.
I tried the same example in localhost (with XAMPP) and it's working fine.
What can be the problem?
PD: This is the content of apache2.conf file:
When I try to acces to vpn, it shows me a 404 error, the requested URL was not found on this server.
If you are getting a "404 Not Found" then it would imply that mod_autoindex is not actually installed on your server (consequently Options +Indexes has no effect - although it would seem from your server config that Indexes is perhaps already enabled).
mod_autoindex is the module responsible for generating the directory listings.
I created an .htaccess file with the next content in the root:
Personally, I would create an additional .htaccess file in the /vpn directory instead:
DirectoryIndex disabled
Options +Indexes
And disable Indexes (and set DirectoryIndex) in the root .htaccess file.
NB: RewriteEngine has no place here, unless you are overriding a parent config.
If I try to access the url "domain/vpn"
Note that you should be requesting domain/vpn/ (with a trailing slash). If you omit the trailing slash then mod_dir issues a 301 redirect to append it.

htaccess rule to show my website under a specific path when accessing root domain URL

I have my website inside the following path on my remote web hosting server:
~/public_html/website/app
and need to find a way to show it when typing:
mydomain.com
so without the need of typing the full path:
mydomain.com/website/app/
One solution that came to my mind was to create a index.php file inside my root folder to automatically redirect to ./website/app/:
<?php
header('Location: ./website/app/');
exit;
but then I need to rewrite the URL in order to go from:
mydomain.com/website/app/
to:
mydomain.com
and I did not find a way to do that!
So, what do you think I should do to obtain that result and show my website under /website/app/ when accessing:
mydomain.com
and do not show the /website/app/ folder names?
Thanks a lot!
edit:
I was forgetting that I also have the website/server path so I need a rule that keeps valid the requests containing mydomain.com/website/server/.
Plus, I cannot use:
Options +FollowSymLinks
on my hosting (for security reasons), it's overridden by:
Options +SymLinksIfOwnerMatch
You have two options to do that.
If you just want to show the /website/app directory for your root url / then you can use DirectoryIndex directive .
DirectoryIndex website/app
This will show you the index file from /website/app folder if you visit the your root URI ie : example.com/ .
If you want to server files and directories from the /website/app instead of the / folder then you can use a RewriteRule that rewrites your root level requests to the subfolders .
RewriteEngine on
RewriteRule !website/app /website/appâ„…{REQUEST_URI} [L]

How does a webserver recognize the landing page?

I have a pure HTML website cloned to my local XAMPP server, it is under 'foo' folder. In this folder, there are only .html files and an empty .htaccess file.
When I open URL localhost/foo, the browswer will open localhost/foo/index.html file. So the question is, how does the server know it was to open the index.html file?
If you check the apache conf file for your XAMPP installation (C:\xampp\apache\conf\httpd.conf), you will find the following declaration under DirectoryIndex:
<IfModule dir_module>
DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm \
default.php default.pl default.cgi default.asp default.shtml default.html default.htm \
home.php home.pl home.cgi home.asp home.shtml home.html home.htm
</IfModule>
index.html ends up being the 6th file apache will try to load. You can customize this list, restart apache in the XAMPP control panel and have a different default file load.
This is determine by an Apache configuration DirectoryIndex. Example:
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
# The index.html.var file (a type-map) is used to deliver content-
# negotiated documents. The MultiViews Option can be used for the
# same purpose, but it is much slower.
#
DirectoryIndex index.php index.html index.html.var
The server will open index.html (or index.htm or index.php) when no other page is explicitly requested because that is the default page for every directory on a web site. If you ask for zyx.com\reports.html then you will open the reports.html page because you specified the page you want to view. When you don't ask for a specific page (i.e. you enter zyx.com in the address bar) the default page for that directory is the index page, so that is what opens. What happens if you type zyx.com and there is no index page created for that directory? The server will return a listing of all the files in that directory, displaying links to all documents and/or images. There will also be a link to the parent directory so, in practice, a viewer would be able to access your files to find things of interest.
Mostly servers support index.html as default file. But if you have any other file which you want to make it as landing page. You may change you htaccess
DirectoryIndex filename.extension
You can add multiple files there like following example. In this case anyone of theme present will be landing page. If all present then first will be given priority.
DirectoryIndex filename.extension filename.extension filename.extension

Rewrite subdirectory to index.html

I have a subdirectory setup with a static website inside.
But when I go to www.site.com/directory/ it doesn't show the index page, only www.site.com/directory/index.html works.
Is there a way for .htaccess to rewrite this?
I can't just forward all urls because there are also other pages like www.site.com/directory/other.html that still need to work.
Inside /directory/ it's enough that you put an .htaccess file with the directive:
DirectoryIndex index.html
If it still does not work, maybe there are other server directives conflicting with it.
In Apache config you should enable AllowOverride so you could set different options in .htaccess. In that file you could set the DirectoryIndex directive.
See:
http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryindex
http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride

How to change the default index.html to index.php when you have two index files

I want to have two index files. one - index.php and one - index.html.
But I want also to make the default file on the server - index.php
(I have two file just on local environment).
I've already tried to add in the .htaccess the row : DirectoryIndex index.php
but the index.html still loaded first.
Is there any solution without deleting one of the files.
You can specify multiple values:
DirectoryIndex index.php index.html
In order for this to work you need to allow overrides for indexes in the apache config
AllowOverride indexes
http://httpd.apache.org/docs/2.2/mod/mod_dir.html
A common error is that your .htaccess file isn't being read at all. A good way to make sure your .htaccess is being read is to put a typo in it and make sure you get an error.

Resources