Hide your website subdirectory in your URL

October 21, 2009

If you have a website in a sub directory and want to point your main URL to it, here's a trick that worked for us.

We had a Joomla site installed in a sub-directory called "home".  This was sitting next to another directory called "dev".  The idea was to have 2 copies of the same website sitting next to each other:  a "development" site for testing, and a "live" production site.

Scanning the web, we found many people offered the following solution, which involves creating a file called ".htaccess" with the following code:

RewriteBase /

# Add trailing slash if path does not contain a period or end with a slash
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) http://www.yoursite.com/$1/ [R=301,L]

#Change http://yoursite.com to http://www.yoursite.com (Optional)
RewriteCond %{HTTP_HOST} ^yoursite.com$
RewriteRule ^/?(.*)$ http://www.yoursite.com/$1 [R=301,L]
#Rewrites http://www.yoursite.com/subdir to http://www.yoursite.com/
RewriteCond %{REQUEST_URI} !^/subdir
RewriteRule ^(.*)$ subdir/$1 [L]

 

For the above code, replace yoursite.com with the name of your website & subdir with the sub-directory (e.g. where Joomla was installed).

Then you upload the .htaccess file into your website's home directory (note that there is a dot "." at the beggining of the filename — this is normally required, so if your computer doesn't support this type of file name, name it "htaccess" without the dot, and then use your FTP program or webhost's control panel and you should be able to change the name there).

Nearly every place we found these instructions left our a few extra steps which are important to do:

  1. Make sure your .htaccess file is set as executable (assuming you are running this on a shared UNIX/Linux host).  Most FTP programs support changing the file attributes.  Good rule is to make it user and group executable first, and if that doesn't work, make it world executable.
  2. Most important, make sure your Joomla configuration.php file has the following set:
    var $live_site = '';
    To
    var $live_site = 'http://www.yoursite.com/';
    (Replace yoursite with the name of your website)

For us, it was the 2nd step that made the code work for us!!!

One last point, if you also use Joomla's built-in search engine frienly URL setting using Joomla's .htaccess file (in your Joomla subdirectory), the code in that should work fine with the other .htacess file you put in your host's root directory.  For your Joomla's .htaccess, make sure to us:  RewriteBase /

Enjoy!

  • LinkedIn
  • Facebook
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
  • MySpace
  • Digg
  • Live
  • Slashdot
  • RSS

3 Responses to Hide your website subdirectory in your URL

  1. help stop drinking on June 11, 2010 at 3:27 am

    I’ve recently started a blog, the information you provide on this site has helped me

  2. NextTrak on June 24, 2011 at 3:44 pm

    Thanks for the information for “Moving the site among directories/sub-directories”.

    I have also found the link of Joomla docs as given below as it seemed to be more easy.

    http://docs.joomla.org/Moving_the_site_among_directories/sub-directories

  3. Buz on August 1, 2011 at 11:05 pm

    That Joomla link is good for permanently moving your directory. But it does help having a quick way to switch where your web address points to.

    I think the most practical use is when you are testing a new version of your website, such as a Joomla site. Say you’re running Joomla 1.5 and want to upgrade to Joomla 1.6. You can have a directory “joomla_1-5″ and “joomla_1-6″ sitting next to each other. Using htaccess, you can “point” to which directory you want to use for your live site. With this approach, you can easily switch between sites, which can be useful if you need to quickly switch back to your old side.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

Subscribe to email updates

About the Author

Buz has been a consultant since 1998, advising businesses on technology and digital strategy.

TwitterFacebookRSSLinkedIn

Twitter