Apache Redirect Root URL to Subfolder

Apache Redirect Root URL to Subfolder

Get Social!

The Apache HTTP is able to redirect traffic to a specific URL with use of the Apache mod_rewrite. mod_rewrite can do at least 100 other things and I’ll include some of those in a later blog post.

Let’s take a look at a simple redirection of traffic from / to /mysubfolder.

For example, this would redirect all traffic sent to http://www.jamescoyle.net/ to http://www.jamescoyle.net/mysubfolder/

This can be very helpful when you are using a reverse proxy and the application you are proxying is on a sub folder in the URL path. You can simply use this technique to redirect all users to the subdirectory folder path.

Make sure the module is enabled. In Ubuntu you can simply run the a2enmod command however in RHEL/ Cent OS you may need to add the module manually to your httpd.conf file.

a2enmod rewrite

Then in your sites file you will need to add the following code.

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /mysubfolder/ [R=301]
  • RewriteEngine on is used to specify to Apache that this site will use Rewrite rules to transform the URL
  • RewriteCond is the match part of the pattern. If the URL matches this pattern then RewriteRule will be applied. This particular pattern is checking if the requested URL path is equal to /.
  • RewriteRule is going to add /mysubfolder/ to the URL which only contains the domain due to the above RewriteCond already performing the check.

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]

 


Visit our advertisers

Quick Poll

Do you use GlusterFS in your workplace?

Visit our advertisers