Mod_Rewrite Tips: Redirect Site root to subdirectory

Written by Marcus Miller on . Posted in Blog

Looking to redirect your site root to a subdirectory? Whatever the reason, this is easily acheived with the every useful mod_rewrite.

I’ll assume you know what you are doing to some degree and that your .htaccess file is in place and you know mod_rewrite is working as it should.

Redirect Root to Sub Directory

The basics here are pretty simple.

Firstly, you have a negative condition (prefixed with !) that tells the webserver to ignore all requests that are made to the subdirectory. Then we set up a simple redirect for all other requests (.*) to redirect to the subdirectory.

RewriteCond %{REQUEST_URI} !subdir/
RewriteRule (.*) http://www.yoururl.com/subdir/$1 [L]

* obviously, replace ‘yoururl.com’ & ‘subdir’ with the relevant values, duh! (sorry, but the question has been asked)

That’s it – pretty simple hey.

But, my images (etc) are no longer working…

If you have images or any media (pdfs, flash etc) that are in the site root or a subdirectory below where we are redirecting all requests to, you will have a problem that these are no longer being displayed.

The reason for this is that you are redirecting all requests for imagedir/imagename.jpg to blog/imagedir/imagename.jpg and therefore creating a broken link.

Fortunately, this is pretty easily rectified by adding another rewrite condition for each folder you want to ignore. So, assuming we had images in the imagedir/  folder we would add the following rule.

RewriteCond %{REQUEST_URI} !subdir/
RewriteCond %{REQUEST_URI} !imagedir/
RewriteRule (.*) http://www.yoururl.com/subdir/$1 [L]

So now, much like requests made directly to the subdirectory we are ignoring requests made for the images in the www.yoururl.com/imagedir folder. You can add as many of these conditions as you need to ensure the rewrite rule only triggers when you need it.

Testing, testing, testing

For those of you who made it this far (woot, it works, next job) – this is easy enough for a small site, but once you have made the change you need to test to make sure all your pages and images are working site wide. Best way to do this is with the venerable Xenu Link Sleuth.

Temporary, Permanent?

The examples above will issue a HTTP 302 temporary redirect by default. If you want to issue a permanent redirect (301) then add R=301 to the rewrite rule.

RewriteRule (.*) http://www.yoururl.com/subdir/$1 [L,R=301]

If you want to test the HTTP headers then you can do a lot worse than web bug.

Note

If you are looking at ignoring the site root permantely, long term you may be better moving the whole site to the root and rewriting all requests for subdir/file.xxx to /file.xxx.

If you need help with your mod_rewrite issues follow me on twitter

Be Sociable, Share!

    Marcus Miller

    Marcus has been creating and marketing websites since, well, lets just say before the Millennium and is currently focused on Internet Marketing and SEO. If you have a problem, and no one else can help, and if you can find the contact page, Marcus can help. Email | Twitter | Google+

    Comments (4)

    • GF

      |

      Hi Marcus

      I’m experiencing some problems with a Magento webshop. I have it in several languages: .com/fr (for french), .com/nl (for dutch, main language) and .com/en (for english).

      First problem: .com and .com/nl are the same, in other words duplicate content. Magento does this automatically (store code)

      Second problem: when you’re browsing on .com/fr, then quit the website and then go back to .com … the .com is suddenly in French!

      For SEO purposes, would it be a good solution to have htaccess force a redirect from .com to .com/nl? Or something else?

      Reply

      • Marcus Miller

        |

        Hey, do you want to fire me an email with a link so I can take a quick look and give you some five minute feedback?

        Reply

    • Jens

      |

      This can be accomplished much easier. Without the hassle of adding more rules to exclude specific directories.

      You can use a rewrite rule that uses ^$ to represent the root and rewrite that to your subdirectory, like this:

      RewriteEngine On
      RewriteRule ^$ /subdir [L]

      Reply

      • Marcus Miller

        |

        Hey Jens, very cool, fancy doing a mod rewrite guest post? This one is three years or so old so could do with an update. Cheers! Marcus

        Reply

    Leave a comment