Sometimes you need to redirect your top level domain (or TLD, or simply your main domain) to point to a website located in another sub-directory or sub-floder, either temporarily or permanently. To do this:
- Browse to your main folder or root domain directory (for example, this can normally be inside public_html)
- Find if you have an .htaccess folder. If you are browsing inside CPanel’s File Manager, make sure you have ticked or selected to show hidden files. .htaccess files (or any other files starting with a dot in the filename) will not show-up in the web-based file manager.
- If you can confirm you do not have an .htaccess file, create one and put the below code block inside, save, and re-upload to your root/main website folder.
- If you found an existing .htaccess file, edit it with the same code block, save and re-upload as needed. Important! Backup your original htaccess first before modifying, so you can rollback if needed.
- Go back to your browser and reload your webpage using your main domain. This will now serve contents from your indicated subfolder, while retaining or using the same top level domain. You may need to clear your browser cache.
NOTE: To use this block of code, you need to change instances “example.com” to your domain and instances of “subdirectory” to your sub-folder name. Code lines beginning with the pound sign (#) are comment lines or notes that can modified for your guidance.
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subdirectory/
# Don't change the following two lines.
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 example.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.)?example.com$
RewriteRule ^(/)?$ subdirectory/index.html [L]