How to serve main Domain Name from a subdirectory?

Assumptions

• Your domain name is → MyDomain.com
 You have a cPanel (hosting)account with this domain name as a main domain.
• Currently this domain name is serving its files from the directory public_html.

Your Goal:

• Serve this domain name(mydomain.com) from a directory named "mydirectory" inside the public_html, (public_html/mydirectory).

HowTo:

• Go to the cPanel account, at the 'Files' section, click 'File Manager', this will take you to the file structure/management of your account.

• Now, at the upper-right of the window you see an icon 'Settings', click the icon, and make sure the 'Show Hidden Files' is marked/ticked, then click Save.
• After doing the change above, the (dot) files should show on the folders, you have to look inside public_html for a file called '.htaccess' - If the file is there, you click on it, and then click on 'Edit' at the toolbar at the top. If the file is not there, we have to create it.

• How to create the '.htaccess' file: On the same window as for the 'Settings' above, is an icon at the upper-left '+File' - when you hover the mouse over it, it will say 'New File', click there and a pop-up window will come up, with two fields. The first field is labeled 'New File Name:', the second field is labeled 'New file will be created in:' - these fields should have the values as below, and then saved, by clicking 'Save'.

New File Name: .htaccess
New file will be created in: /public_html

(click Save).

• Now click/select the '.htaccess' file, then click 'Edit' at the toolbar. - Now you have to insert the code/commands below as advised on the comments above the code/commands. Comments are marked with : #

• .HTACCESS code/commands (value) :

# If this line below is on the .htaccess file, leave it like it is, otherwise add it.
RewriteEngine on 

# The domain name 'example.com' below, should be changed to your domain name, in our case mydomain.com. 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 

# The 'subdirectory' below should be changed to the directory your domain name will serve from, in our case 'mydirectory'. 
RewriteCond %{REQUEST_URI} !^/subdirectory/

# Following lines should not be altered. 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d

# The 'subdirectory' below should be changed to the directory your domain name will serve from, in our case 'mydirectory'.
RewriteRule ^(.*)$ /subdirectory/$1

# The domain name 'example.com' below, should be changed to your domain name, in our case mydomain.com.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 

# The 'subdirectory' below should be changed to the directory your domain name will serve from, in our case 'mydirectory'.
# The 'index.html' file below, should be left as it is, if this is the main file of your website, otherwise it should be changed to 'index.php' or whichever file is the main file of website, in our case the main file of website is 'index.php'. 
RewriteRule ^(/)?$ subdirectory/index.html [L]

• So based on our assumptions above, the code/commands (value) of .HTACCESS file, in total, will look like below(changes are in bold.):

RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{REQUEST_URI} !^/mydirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mydirectory/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteRule ^(/)?$ mydirectory/index.php [L]

Finally, save changes, and you'r done.

  • 2 Users Found This Useful
Was this answer helpful?

Related Articles

New gTLD's

Q: What is a gTLD?gTLD stands for generic Top-Level Domain (what Internet users see as an...

How can I change the nameservers for my Domain Name?

Assuming your domain name is registered with nativespace, you can change the nameservers in the...

How can I change the contact or other information for my Domain Name?

Assuming your domain name is registered with nativespace, you can change the contact (or other)...

What is the TAG or IPSTAG for transferring domain names to nativespace?

When transferring any .uk domain names to nativespace, you will need to change what is known as...

I transferred a domain name to nativespace, but my site is not appearing!

Note that when we transfer a domain name, we do not modify the nameserver information. You must...