Change the current document root to a new directory inside the current one using .htaccess

The scenario is as follows. Client is having a website mydomain.com which is the primary domain of the cpanel account. Now the user want to redirect all the traffic coming to the current document root (/home/username/public_html) to the new_folder created (/home/username/public_html/new_folder/) for the site. Create a .htaccess file in /home/username/public_html with the below contents :

RewriteEngine on

# Change mydomain.com to be your primary main domain.
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$

# Change 'new_folder' to be the folder you want to redirect request to.
RewriteCond %{REQUEST_URI} !^/new_folder/

# Don't change this line.
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d

# Change 'new_folder' to be the folder you want to use for your primary domain.
RewriteRule ^(.*)$ /new_folder/$1

# Change mydomain.com to be your primary domain again.
# Change 'new_folder' to be the folder you will use for your primary domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteRule ^(/)?$ new_folder/index.php [L]

It is always recommended to change the document root directly in apache or from WHM rather than using this temporary solution. Still worth to know. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s