Mod_Rewrite Tips: Redirect Site root to subdirectory

Looking to redirect your site root to a subdirectory? Whatever the reason, this is easily achieved 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 web server 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. The best way to do this is with the venerable Xenu Link Sleuth.

 

Temporary, Permanent?

 

The examples above will issue an 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 permanently, 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

 

Facebook
Twitter
LinkedIn
Pinterest

Leave a Reply

Your email address will not be published.

Free Digital Marketing Review

Book a call with Marcus Miller to learn how to generate more traffic, leads and sales.

1. Review your current marketing
2. Identify your key opportunities
3. How to improve results & cut costs!

Put over 20 years of marketing experience to work for your business today.