Redirect through .htaccess file

Here’ s the logic to redirect particular page through .htaccess file.

RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

as given above,
here is the url of the site you want to redirect. here if the url typed in the address bar is, it will redirect to

the above given logic will work for apache webserver only as .htaccess file is supported by apache webserver and not IIS webserver. In case you have an IIS webserver here’s something you can do.

But this logic has to be written in php file. The logic follows as below:


header( “HTTP/1.1 301 Moved Permanently” );
header( “Location: http://www.&#8221;.$_SERVER[‘HTTP_HOST’].$_SERVER[‘PHP_SELF’].’?’.@implode(‘&’,$_SERVER[‘argv’]));
header( “Location: http://www.&#8221;.$_SERVER[‘HTTP_HOST’].$_SERVER[‘PHP_SELF’]);




Creating virtual host on apache, php

You can create a virtual host on your local system or your sever. i.e. you can have http://trial/ instead of http://localhost/trial/ where trial is the name of the directory that contains the website.

Here are the steps for creating virtual host on your sever:

1) open the host file and edit it as follows:

the host file is found at C:\WINDOWS\system32\drivers\etc\hosts for windows

open the file and add the following code to the bottom of the file on a new line       trial

2) Now open the httpd.conf file under the apache/bin/conf/ directory

and add the following code :  # Virtual hosts
Include conf/extra/httpd-vhosts.conf

or if already present then please uncomment the following line
: Include conf/extra/httpd-vhosts.conf

i.e. remove the # from the front of the line

3) Open apache/bin/conf/extra/httpd-vhosts.conf file

if not present create directory extra under

and create a file httpd-vhosts.conf file

and add this code to the file:

# Virtual Hosts
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn’t need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
# Please see the documentation at
# <URL:;
# for further details before you try to setup virtual hosts.
# You may use the command line option ‘-S’ to verify your virtual host
# configuration.
<Directory D:/wamp/www/vhosts>
Order Deny,Allow
Allow from all
# Use name-based virtual hosting.
NameVirtualHost *:80

# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.

#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.localhost
#    DocumentRoot “C:/Program Files/Apache Software Foundation/Apache2.2/docs/dummy-host.localhost”
#    ServerName dummy-host.localhost
#    ServerAlias http://www.dummy-host.localhost
#    ErrorLog “logs/dummy-host.localhost-error.log”
#    CustomLog “logs/dummy-host.localhost-access.log” common

#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host2.localhost
#    DocumentRoot “C:/Program Files/Apache Software Foundation/Apache2.2/docs/dummy-host2.localhost”
#    ServerName dummy-host2.localhost
#    ErrorLog “logs/dummy-host2.localhost-error.log”
#    CustomLog “logs/dummy-host2.localhost-access.log” common

<VirtualHost *:80>
DocumentRoot D:/wamp/www
ServerName localhost
<VirtualHost *:80>
DocumentRoot D:/wamp/www/vhosts/trial
ServerName trial

if the file is present then please add the following lines at the bottom of the file

<VirtualHost *:80>
DocumentRoot D:/wamp/www
ServerName localhost
<VirtualHost *:80>
DocumentRoot D:/wamp/www/vhosts/trial
ServerName trial

4) restart your apache services and just type http://trial/ into the address bar of your browser and it will work

If you face any problem with this please feel free to contact me. I’ll try to resolve your problem as soon as possible.


Sachin (