Saturday, July 4, 2015

Configure Apache Rewrite Engine

Rewrite Engine is a software located in web application framework running on Apache sever.Users use this feature as a router.This modification is called URL Rewriting.
URL rewriting can be one of the best and quickest ways to improve the usability and search friendliness of your site.

In the begin lets take a example,you have a service page.Its URL is

http://www.spreads.com/service_value/getamount_of_next_index.jsp

but now we want to clean up this url to

http://www.spreads.com/service_value/getamount

we need to tell the server to internally redirect all requests for the URL "getamount" to "getamount_of_next_index.jsp"

To accomplish that we have to configure rewrite engine.By Default apache does not have rewrite engine on.We have to configure it to work. Lets see how to enable rewrite engine in ubuntu installation.I assume that you already have ubuntu and up and running Apache

 1. create .htaccess file in /var/www/ directory with your specific rewrite rule.


   
 2. Requred to enable mod_rewrite module.Use following command to enable.

  sudo a2enmod rewrite

The above Apache2 Enable Module command will add the correct line in the /etc/apache2/apache2.conf file. That is the only change you need to make with the apache2.conf file. Now it's time to make a change to the document root.  
There are some configuration changes with in apache versions.In older versions of Apache all virtual host directory directives were managed in the /etc/apache2/apache2.conf file.Now [Apache/2.4.7] This has changed. Now these alterations are handled within the /etc/apache2/sites-enabled/ directory.

3. Within that directory you will find, by default, a single file called 000-default.


First look in the <Directory /> section and change the line:
 AllowOverride None
 to
 AllowOverride All

Then look the  <Directory /var/www/> section.Do the same thing in here.Some times if there was not that kind of directory tag.Then you have to add following directory tag.

   <Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
    Order allow,deny
    allow from all
   </Directory> 

4.Once you have edited the file,then restart the apache2 server.

sudo service apache2 restart

If you want to recheck weather rewrite module is enable or not.Follow the command.
sudo a2enmod rewrite.
If it is already contains,it will appear Enabling module rewrite.

Now you can test your own codes.






No comments:

Post a Comment