How does a webserver recognize the landing page? - .htaccess

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

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.

How to serve somefile.pdf instead of index.html as default with .htaccess file?

I want visitors on www.domain.com to be served with a pdf file on www.domain.com/file.pdf automatically.
The file shoudl be shown in the browser. Not downloaded. Is this possible to do with .htaccess rules?
My webhoster runs apache.
Put this code in root directory .Htaccess :
DirectoryIndex file.pdf

external site using my css and image files, can I block this

each month many gigabytes of traffic on my site is being consumed by various sites, mostly in italy according to my stats. On further review of my raw access logs, they have built in direct reference links to css and png files in my site. I can just rename them but that wont stop them accessing a page in my site to get their full urls and keep accessing them.
Is there a way to block access to css and image files in my site unless they come from the same domain, somehow?
Regards
Greg J
Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs) (edit the name of the domain):
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^yourdomain\.com
RewriteRule (?:jpg|png|gof|css) - [F]
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

How can I replace index.html by a different file in .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

using htaccess how can I specify index.html as the home page with both index.html and index.php on the server?

As my title says, using htaccess how can I specify index.html as the home page with both index.html and index.php on the server ? I have a regular basic html site on the server and I want to start working on a joomla site which of course uses php. I don't want to redirect the php if I enter in the index.php into the address to allow me to start working on the joomla installation. I just want to automatically go to index.html when the domain name is entered. I hope I'm making sense. Thanks
You use DirectoryIndex to specify, which file is shown, when just the directory is requested
DirectoryIndex index.html

Resources