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:
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.
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