Thursday, July 17, 2014

Apache Rewrite URLs

Share it Please
In many cases , applications deployed into production servers will be accessed with a specific URL rather then accessing the same application with localhost Url. These Url are configured specifically for every application deployed into the production machines. In this article we will see the basics of configuring the Apache Rewrite module which will help in rewriting normal Url to a different one.

For this demo purpose, I will be using 2 sample application written by and deployed in tomcat which are being accessed normally using


Now I want to configure different URL for both these applications which the below URL


Now for the Rewrite to work , we need to start making changes to the httpd.conf file for the apache server.

1. The first thing we need to do is the enable the mod_rewrite module by adding,
LoadModule rewrite_module modules/
LoadModule proxy_module/

2.Now create a VirtualHost element for out configuration. Here is the configuration that I have used for this article

<VirtualHost *:80>
   DocumentRoot /var/www/webroot/ROOT

   ServerAlias *
   RewriteEngine On

   RewriteRule ^/application1/(.*) http://localhost:8080/myApp/ [P]
   ProxyPassReverse /application1/ http://localhost:8080/myApp/

   RewriteRule ^/application2/(.*) http://localhost:8080/Sample-app/test.html [P]
   ProxyPassReverse /application2/ http://localhost:8080/Sample-app/test.html

   RewriteLog "/var/log/httpd/rewrite.log"

   ErrorLog logs/apache-rewrite-test-error_log
   CustomLog logs/apache-rewrite-test common

In the above snippet ,

  • RewriteEngine On enables rewriting ability
  • RewriteRule and ProxyPassReverse specify the conditions and the result of rewriting for both applications
  • RewriteLog is optionally added to store the logs of rewriting in the specified location

So if we take the statements

   RewriteRule ^/application1/(.*) http://localhost:8080/myApp/ [P]
   ProxyPassReverse /application1/ http://localhost:8080/myApp/

The first line tell you that when ever a request comes for application1 in the URL path, redirect  the request to the backend servers and access http://localhost:8080/myApp/ URL from the back end servers.

Once the changes are done, restart the back end tomcat and apache server.

Now access the application using, http://localhost:80/application1 which will access the application from tomcat.

Issues faced:
While working with the above article ,I have seen the below exception in the apache httpd server logs files

(13)Permission denied: proxy: HTTP: attempt to connect to (localhost) failed – this error is because of httpd/apache2 has been denied permission to connect to IP address and port.

The main reason for this is the SELinux , The SELinux in here is not permitting httpd/apace2 to make a network connections.

We can resolve this disabling the SELinux or we can allow the httpd to make network connection like

Run either of following command to allow SELinux to permit httpd/apache2 to make network connections:

$ /usr/sbin/setsebool httpd_can_network_connect 1
$ /usr/sbin/setsebool httpd_can_network_connect true

Then restart httpd/apache2.

More to learn , Happy Learning.

No comments :

Post a Comment