What they are/How to use them
.htaccess
files (or 'distributed configuration files') provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.
Note:
The httpd.conf is a configuration file. It is used by the Apache HTTP Server. Apache server looks at this file for different configuration properties. It is stored in path: /etc/httpd/conf/httpd.conf. Apache web server configuration file httpd conf.DocumentRoot varwwwhtml DirectoryIndex indexhtml save the file and Creation of webpage. The location of this file is set at compile-time but may be overridden with the -f command line flag. /etc/apache2/apache2.conf: Apache’s primary configuration file, which stores its global configuration settings. Other files in the configuration directory are loaded from this file. It also stores the FollowSymLinks directives, which control configuration enabling and disabling.
If you want to call your .htaccess
file something else, you can change the name of the file using the AccessFileName
directive. For example, if you would rather call the file .config
then you can put the following in your server configuration file:
In general, .htaccess
files use the same syntax as the main configuration files. What you can put in these files is determined by the AllowOverride
directive. This directive specifies, in categories, what directives will be honored if they are found in a .htaccess
file. If a directive is permitted in a .htaccess
file, the documentation for that directive will contain an Override section, specifying what value must be in AllowOverride
in order for that directive to be permitted.
For example, if you look at the documentation for the AddDefaultCharset
directive, you will find that it is permitted in .htaccess
files. (See the Context line in the directive summary.) The Override line reads FileInfo
. Thus, you must have at least AllowOverride FileInfo
in order for this directive to be honored in .htaccess
files.
Example:
Context: | server config, virtual host, directory, .htaccess |
Override: | FileInfo |
If you are unsure whether a particular directive is permitted in a .htaccess
file, look at the documentation for that directive, and check the Context line for '.htaccess'.
Using Apache Virtual Host, you can run several websites on the same server.
For example, I can run both thegeekstuff.com and top5freeware.com on a single physical server that has one Apache webserver running on it.
Fig: Apache Virtual Host (Multiple websites, one Apache)
Apache Web Server Tutorial
There are two types of Apache virtual host configurations: 1) IP-Based Virtual Host and 2) Name-based Virtual Host. Name-based virtual host is recommended for most scenarios.
IP-Based Virtual Host
In this configuration, when you are pointing two websites (with different ip-address) to the server that runs Apache, that physical server should have two different ip-address configured.
Apache Web Server Configuration File Format
This means that the server should have two ethernet cards, each one of them configured to the ip-address of the corresponding website that Apache virtual host will be serving. So, this is not practical for most aspects, and you should not be using this.
In the following example, the server contains two NIC cards, one is configured with 192.168.101.1 ip-address for thegeekstuff.com, another is configured with 192.168.102.1 for top5freeware.com. Both these ip-address are served by a single Apache webserver running on that server using IP-Based virtual host.
Name-Based Virtual Host
In this configuration, when Apache webserver receives a request, it looks for the hostname in the HTTP header, and depending on the hostname, it servers different websites. This is very easy, as you need only one ip-address on that physical server; but, you update the DNS with multiple website names pointing to the same ip-address. For all practical purpose, you’ll be using only Name-based virtual host configuration.
In the following example, the server contains only one NIC card, which is configured with 192.168.101.1 ip-address. The DNS entry for both thegeekstuff.com and top5freeware.com website points to 192.168.101.1 ip-address. When Apache recives a request, it looks for the hostname entry in the HTTP header, and serves the corresponding website.
Fig: Apache Name-Based Virtual Host
1. Uncomment httpd-vhosts.conf in httpd.conf
If you’ve installed Apache 2 from source, by default, the following line will be commented in the httpd.conf file. Uncomment this line.
2. Setup virtual hosts
Modify the httpd-vhosts.conf as shown below to setup named-based virtual host setting for two hosts.
- NameVirtualHost *:80 – Indicates that all the name-based virtual hosts will be listening on the default port 80
- <VirtualHost *:80> </VirtualHost> – Enclose all the apache configuration parameters for each and every virtual host between these VirtualHost tags. Any apache directives can be used within the virtualhost container.
- In the following example, we are setting up virtual host for thegeekstuff.com and top5freeware.com listening on the same port 80. So, there will be two <VirtualHost *:80> </VirtualHost>, one for each website.
- When you go to thegeekstuff.com, the files under /usr/local/apache2/docs/thegeekstuff will be served by Apache; and the access_log and error_log for this site will go under /usr/local/apache2/logs/thegeekstuff
3. Check VirtualHost Configuration Syntax
Verify virtual configuration syntax using “httpd -S” as shown below. When everything is setup properly, it just displays “Syntax OK”.
When something is not configured properly, it will display warning message, including “directory does not exit” message as shown below.
4. Restart the Apache and test
Now, when you go to thegeekstuff.com (or www.thegeekstuff.com), the apache will serve the files from /usr/local/apache2/docs/thegeekstuff directory.
When you go to top5freeware.com (or www.top5freeware.com), the same apache running on the same server will serve the files from /usr/local/apache2/docs/top5freeware directory.
Just to reiterate, for the name-based virtual host to work properly, the DNS entry for both these websites should be pointing to the same external ip-address of the physical server where the Apache webserver is running.