Transparently redirect your root directory to a subdirectory

From DreamHost
Jump to: navigation, search

If you would like install something using the one-click manager, it wants you to use an empty directory. If you want to use the root of your web directory, then you have to delete everything on your domain. A better solution is to use a new directory for your installation (wordpress, joomla, etc) and then transparently redirect all request to that sub directory. So for example, any request for any file or folder at http://example.com would forward to http://example.com/cms, excluding files and folders that already exist. So, if someone requested http://example.com/blog it wouldn't forward to http://example.com/cms/blog unless the directory "blog" already existed in your domain's root directory..


Configuring mod_rewrite to transparently redirect requests

In order to do this you'll need to create an .htaccess file with the following content:

Options -Indexes +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^/cms/

# Don't apply to URLs that go to existing files or folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all those to insert /folder
RewriteRule ^(.*)$ /cms/$1 [L]

This .htaccess file above assumes the subdirectory to which you want to redirect all traffic is /cms/. If it is not you will need to change the two references to the cms directory. Save this .htaccess file in the root of your domain's directory (e.g. /home/YOUR_USERNAME/example.com/). If you need help creating and uploading the .htaccess file, please refer to the .htaccess page.

If you want to redirect independently if you have existing files or folders just create an .htaccess with the following content:

Options -Indexes +SymLinksIfOwnerMatch 
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^/cms/

# Rewrites all URLS [Replace "domain" with the actual domain, without the TLD (.com, .net, .biz, etc)]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.

# Rewrite all those to insert /folder
RewriteRule ^(.*)$ /cms/$1 [L]

Redirecting in WordPress

This feature is actually built in to WordPress, and their official directions on their codex work well for Giving WP it's own Directory from a pre-existing subdirectory.


Configuring Joomla to be compatiable with the mod_rewrite technique

  • In Joomla 1.0 you will need to modify the $mosConfig_live_site variable in the configuration.php file located in your Joomla directory to equal your domain name (e.g. $mosConfig_live_site = 'http://example.com';).
  • In Joomla 1.5 you will need to modify the $live_site variable in the configuration.php file located in your Joomla directory to equal your domain name (e.g. $live_site = 'http://example.com';).

Known issues with Joomla

sh404SEF

sh404SEF will give a 404 error for every page when using the .htaccess method.

sh404SEF 404 Work Around

Create a index.php in the root of your domain (e.g. /home/YOUR_USERNAME/example.com/index.php) with the following code:

<?php
include('cms/index.php');
?>

Note: You may need to change "cms" to the directory you used for your Joomla Installation.