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.

Create a default nginx site rule

Tags :

Category : How-to

Get Social!

nginx-logoUsually a web server can be accessed by multiple paths, such as the DNS entry of the server (eg. as www.jamescoyle.net) and the IP address of the server (eg. This is a problem when it comes to presenting a single entry point to your website.

If you use Nginx to serve your website you can add a ‘catch-all’ entry to respond to requiests which do not match an IP based or server_name attribute.

Let’s say you had an Nginx sites-enabled site which responded to www.jamescoyle.net specified by the server_name attribute.

server {
        listen       80;
        server_name  www.jamescoyle.net;
        location / {

Without a catch all entry, all requests to the server would be served as if they were requesting www.jamescoyle.net when in fact they could be using the web server IP address. Ideally, we want to handle all requests coming to www.jamescoyle.net and redirect any other requests to www.jamescoyle.net. The default_server statement in the listen command specifies that this server code block will respond if no other code blocks do. In the above example, that code block will handle all reuests to www.jamescoyle.net and all other requests will be issued a redirect.

The following code will redirect all unanswered requests to http://www.jamescoyle.net.

server {
    listen 80 default_server;
    return 301 http://www.jamescoyle.net;

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:


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

What type of VPN protocol do you use?

Visit our advertisers