Articles

Moving Your Primary Domain to a Subfolder with .htaccess

Move Primary Domain to Subfolder

Every cPanel hosting account will be assigned a Primary Domain; that’s the nature of the cPanel control panel. Your account’s Primary Domain is the domain name you signed up with when you first placed your order here at PeoplesHost.

For customers with multiple addon and subdomains, the folders or Document Root for each addon and subdomain may sit inside of the Primary Domain’s public_html.

Please keep in mind that addon and subdomain directories aren’t always placed inside the Primary Domain’s public_html directory. It depends where you’ve defined the Document Root when you first created the addon or subdomain inside of cPanel.

If your .htaccess already contains existing rewrite rules you may want to consider NOT moving your Primary Domain into a subfolder. This may cause or lead to issues that break your site.

  1. Create a New Subfolder
  2. Move Site Files to New Subfolder
  3. .htaccess Code Example
  4. Updating Your .htaccess

1. Create a New Subfolder

The first step in this process is to create a new subfolder to move your Primary Domain’s files into. You’ll want to create a new subfolder using File Manager or FTP (File Transfer Protocol). In this walk-through we’re going to use File Manager, which can be accessed inside of cPanel.

Log into cPanel and locate the File Manager icon under the FILES heading. A new tab will open inside of your browser taking you inside of the File Manager.

You should see a folder labeled public_html. Double click this folder. This is where your website’s files are stored.

Click the + Folder link located in the top navigate bar (top left). Name this folder yourdomain.com where yourdomain.com is replaced with your actual domain name.

You will now see a new folder created inside of your public_html. It should look something like the image below:

2. Move Site Files to New Subfolder

The next step in this process is to move your Primary Domain’s files into the new subfolder you just created.

Click the Select All link to select all files in the public_html. You’ll want to make sure you deselect the new subfolder you just created. You can deselect the subfolder by holding CTRL on your keyboard and hitting the left click button on your mouse.

Now, you have two options. You can either drag these files into the new subfolder you created or use the Move functionality inside of File Manager.

To use the Move functionality, click the Move link in the top navgation. A new popup will appear where you’ll want to specify the file path for your new subfolder location. Click the Move File(s) button once you have entered the name of your new subfolder.

You have successfully moved your Primary Domain’s files to a new subfolder.

3. .htaccess Code Example

Here is the code you will want to place inside of your .htaccess file:

# .htaccess primary domain to subfolder
# 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 domain.
RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$

# Change 'subfolder' to be the folder you will use for your primary domain.
RewriteCond %{REQUEST_URI} !^/subfolder/

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

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

# Change yourdomain.com to be your primary domain again.
# Change 'subfolder' to be the folder 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.)?yourdomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]

4. Updating Your .htaccess

Now that you’ve successfully moved your files you’ll need to create or update the existing .htaccess in your public_html directory. You may not be able to see the .htaccess because it’s a hidden dotfile.

If you don’t see your .htaccess, you’ll want to click the Settings button in the upper right corner inside of File Manager.

After you’ve enabled the setting to show hidden dotfiles, you’ll want to navigate back Up One Level to your public_html direcotry.

You should now see the following:

  • The subfolder you created in step 1 that now contains your Primary Domain’s files
  • An .htaccess file you will need to update with the code below

If you do not see an .htaccess file follow these instructions.

Click the + File link in the top navigation upper left corner. A popup window will appear where you will define the new file name. The new file name will be .htaccess (with a . in the front of the name).

Now you’ll need to edit the .htaccess file. Right click the .htaccess file and click Edit. This will open up the File Manager Editor.

Add the code inside of your .htaccess as shown below and click the Save Changes button in the upper right corner.

DO NOT remove the #’s in front of any of the commented lines.

On the last line your index file may not be index.php. For example, it may be index.html. Make sure to use the correct file extension.

After saving your changes to the .htaccess file, website visitors will not be able to tell that your Primary Domain is now using a subfolder. Any request being made to your Primary Domain will be redirected to the subfolder you created.

Looking for reliable Windows or Linux hosting with Award Winning Customer Support
Grab your hosting today with a 20% discount. All plans come with our 60 Money Back Guarantee!

Previous ArticleNext Article