Using .htaccess to set a subdirectory to behave as the root of your website

I ran into a need to do this with a Ruby on Rails site I've been setting up and for the life of me I couldn't figure out how to get the application to read the root directory of my website, I could only get things to run from the /public directory. I really didn't want /public to show up in the address bar all the time, so I searched and searched for a way to redirect and mask the subdirectory using .htaccess, here's how to do it:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /
subdirectory/$1
RewriteCond %{HTTP_HOST} ^(www.)?
domain.com$
RewriteRule ^(/)?$
subdirectory/ [L]

Replace domain.com with the your domain and subdirectory with the subdirectory you would like to act as the new "root" of your site.

Hope this helps anyone else who was searching for a way to do this.

Leave a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About this Entry

This page contains a single entry by published on December 20, 2007 9:44 AM.

Firefish Makes an Appearance was the previous entry in this blog.

Sleeping is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.21-en