How to Set Primary Domain Into a Subfolder on Bluehost

Written on Thursday, February 4th, 2010 | Filed under: Coding

Today, I decided that I needed to move my website, a WordPress, installation to a different location.  Since I was going to move and migrate my entire website, I figured I might as well choose an ideal location.

Being hosted on Bluehost meant that my primary domain resided in the root folder.  Any add-on domains were in their own sub folders or sub directories inside the root or public_html.  The downside to this is it keeps all the files to my primary domain unorganized in the root folder.

In order to have it organized, I wanted to have my primary domain folder reside in its own sub folder or sub directory.  I am not as comfortable in the server environment as my knowledge of working with the .htaccess file is limited.

I searched Bluehost knowledgebase as well as other sources and finally came up with a solution that would work.  It required modifying the .htaccess file. You needed to redirect any traffic hitting your domain name to the sub folder directory.

The code is as follows:

# Bluehost.com
# .htaccess main domain to subdirectory redirect
# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.
# Do not change this line.
RewriteEngine on
# Change yourdomain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subdirectory/
# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subdirectory/$1
# Change yourdomain.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ subdirectory/index.php [L]

Once that was accomplished, I was able to have my files in a sub folder directory organized like the other add-on domains.

As a side note, since my website is using WordPress, it took a bit more delicate effort to get it working structurally.  One of the problems I had after the redirection to my .htaccess file is that WordPress kept rewriting my .htaccess file rendering the redirection code above useless whenever I updated the Permalink structure.

After hours of testing and trying to figure it out, I found my problem.  The solution is to make sure that once you have done the redirect code to your root .htaccess file, the WordPress Address (URI) and Blog Address (URI) both need to match.

For example, even though, my domain is http://yourdomain.com but the WordPress installation is located at http://yourdomain.com/subfolder, both WordPress Address and Blog Address URI’s need to point to the path of http://yourdomain.com.  I made the mistake of pointing my WordPress Address to http://yourdomain.com/subfolder causing a loop or an Internal 500 Server error.

Here are some additional resources for you if you are on Bluehost and also about to do what I just did.  This can save you lots of time, like 4 hours worth.

Tags: , , ,