Free Search Engine Guide

Learn SEO in an hour! Our free SEO guide explains everything you need to know to get your site ranking well in google and the other search engines. It is only 20 pages long and can be read in an hour.

Beginners Guide to SEO

Free Website Planning Guide

Our free website planning guide provides an overview of everything you need to take into consideration when developing or commissioning a website project. Readable in an hour, save weeks of wasted time and bucketloads of cash.

How to plan a website

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

Our Work - A Few Examples

Free Search Engine Guide

Our free SEO guide explains everything you need to know to get your site ranking well in google and the other search engines.

Beginners Guide to SEO

Free Website Planning Guide

Our free website planning guide provides an overview of everything you need to take into consideration when developing or commissioning a website project.

How to plan a website

Not sure where to start? How about some help from a friend?

Websites and doing business on the internet is a confusing business and you can spend lots of money with little or nothing in return. There is no 'one size fits all' solution and you must determine what is right for you, your business and your budget.

Free 30 Minute Consultation

We offer a free 30 minute consultation (a chat really) where we can look at your site or goals, let you know the options available and the best course of action.

Get in touch today and start making the web work for you!