In this article I am going to explain how to configure httpd.conf file at Apache web server level for a single website, the httpd.conf file is the main configuration file for Apache web server  and it allows us to modify or add configuraiton of the apache webserver features and functionalities and you find it under “apache/conf/” folder here, I am going to cover basic features which are required to setup a single website
I have new domain name called and on hitting this domain name it should redirect to greyarea specific index.html document which is placed under htdocs folder of Apache server
First we need to have VirtualHost entry for greyarea domain in httpd.conf file, every VirtualHost entry maps to one website , on a single machine we can have more than one VirtualHost entries and each VirtualHost entry maps to different websites
VirtualHost Configuration :
The below VirtualHost entry configuration in httpd.conf file helps us to setup a confiugaration for website
Here, I am using name-based virtual hosting because the server which I am going to setup is shared to many different hosts
You can change it to IP-based virtual hosting by removing “*” and keeping IP-address “<VirtualHost>”
The DocumentRoot is the directory which contains your greyarea specific document’s ( html, css , js etc… ), any request which is coming from the client the server first looks for that file in the default folder which is specified in “DocumentRoot /Apache/htdocs”
The servername specifies host name here, is the host name

<VirtualHost *:80>
DocumentRoot /Apache22/htdocs
RewriteEngine On
ErrorDocument 404 /htdocs/404.html

The Rewrite Url’s work’s only when the RewriteEngine is On so, we must add “RewriteEngine On” at greyarey specific VirtualHost, we can Off it by setting up “RewriteEngine Off”, but if you make it Off the rewrite url’s which you have written will not work

RewriteEngine Off
RewriteEngine On

What if end user is trying to open non existing page something like this and how can we handle this there the built in feature of Apache server will help us and we need to configure ErrorDocument for 404 status code , we can add ErrorDocument either at virtualhost level or global, the ErrorDocument configuration’s which are added at virtualhost will be applicable to only for partiular website
ErrorDocument 404 /pagenotfound.html
Redirect to home page :
The below confguration will display index page for host entry

RewriteEngine On
RewriteRule ^/$ /greyarea/index.html [PT,L]

Short to long Url :
The below configuration helps us when you enter in the address bar redirect to

  RewriteCond %{REQUEST_URI} !^/greyarea
  RewriteRule ^/(.*)$ /greyarea/$1 [NC,NE,L,R=302]

Friendly Urls :
Actually in the below configuration the blogs folder does not exist but the url which start’s with “/blogs” will automatically map to blog-template folder

RewriteRule ^/blogs/(.*)$ /greyarea/blog-templates/$1 [NC,NE,L,R=302]

Handle QueryString’s :
The url’s which contains key=value will automatically transforms to friendly url’s in order to work this properly the friendly url’s folder path’s must exist

 RewriteCond %{QUERY_STRING} ^(.*)key=(.*)$
 RewriteRule ^(.*)$ [R=302,L]

Build query string based on friendly url’s  :
if you enter “”  in the address then the below configuration will build new url’s something like this “”

 RewriteCond %{REQUEST_URI} ^/greyarea/(.*)/(.*)$
 RewriteRule ^(.*)$ [R=302,L,QSA]

Trim .html extension :
The below configuration trims the .html extension in the url, “” to “”

 RewriteCond %{REQUEST_URI} ^/(.*)\.html [NC]
 RewriteRule ^(.*)$ /%1 [R=302,L]

By coderss

Leave a Reply

Your email address will not be published. Required fields are marked *