Apache – redirect traffic to a different url

Apache – redirect traffic to a different url

Get Social!

Some web applications I work with are only available on a URL similar to http://hostname/application. This causes problems when giving the URL to users as they sometimes forget the /application part and receive an unhelpful page they are not looking for or worse, an error.

Using mod_rewrite in Apache2 we can force any traffic matching a specific URL to another URL of our choosing. For this example, we want to direct users landing on / to /application. Notice these URL strings only need to include the path.

Make sure mod_rewrite is enabled in you Apache2 configuration. On Debian flavour distributions you can use

a2enmod rewrite

For Red Hat type distributions, you need to uncomment the line containing mod_rewrite.so in /etc/httpd/conf/httpd.conf.

A basic redirect matching rule has two components. What URL to look for when redirecting, and where to send the traffic.

Edit the vhost file which you would like to include the redirect. For example:

/etc/apache2/sites-available/default

And add the following inside the <VirtualHost> tags.

RewriteEngine  on
RewriteRule ^[FROM]$ [TO] [R=301,L]

You will need to replace [FROM] with the url you would like to direct and [TO] should be the URL of where to send the user. For example, the below rule redirects users going to / to /myapplication

RewriteEngine  on
RewriteRule ^/$ /myapplication [R=301,L]

 


Leave a Reply

Visit our advertisers

Quick Poll

Do you use GlusterFS in your workplace?

Visit our advertisers