One recurring question affects mostly our webspace packages, but should be interesting for many others as well:
How can I move the content of my main domain to a sub-folder without affecting its appearance?
cPanel always assigns one domain as the main domain for a user account. Add-On domains can be added easily, but they simply appear as sub-folders in the same folder that is being used by the main domain. Here is the effect:
/
- public_html/
- admin/
- config/
- data/
- example2.com/
- admin/
- config/
- data/
- themes/
- index.php
- themes/
- index.php
This can cause confusion when using several add-on domains. It would be better to have the content of the main domain in a separate sub-folder as well. However, cPanel does not offer a configuration option for this purpose. Luckily, this will not be necessary though, since htaccess rewrites allow specifying the actual location of the website content.
It is best to start with a fresh account. Otherwise, a full backup of all files is as always recommendable. The cPanel backup assistant will be helpful here.
The following steps can be done easily via FTP client.
If you are moving an existing website, create a sub-folder in /public_html and name it after the domain, e.g. example.com. Move all content of your main domain to the newly created folder. In our example this would include admin, config, data, themes and index.php. If there already is a .htaccess file, move it as well.
Then create a new .htaccess file in /public_html and paste the following:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !^/example.com
RewriteRule ^(.*)$ example.com/$1 [L]
example.com has to be replaced with your actual domain name.
After saving the file you can open the domain in your browser. The website should be displayed correctly.
The next step depends on the software that is being used on your website. In this example, we show the procedure for Joomla! 3 and WordPress.
Most scripts generate relative URLs based on their location by default. Our changes would cause links on your website to be shown like this:
https://www.example.com/example.com/index.php
To correct this behaviour, the base URL needs to be set statically in the Joomla! configuration. Open configuration.php with an editor and modify the following line:
public $live_site = 'https://www.example.com/';
configuration.php is usually stored read-only. You may need to gain write access first by modifying the file permission.
If you use URL Rewriting in Joomla!, edit the following line in the .htaccess file in the sub-folder of your domain:
RewriteBase /example.com
Make sure to remove the hash at the beginning of the line.
The WordPress configuration is similar. Open wp-config.php with an editor and modify the following lines:
define('WP_HOME','https://example.com');
define('WP_SITEURL','https://example.com');
The changes are not visible to visitors and your website is accessible normally. The new folder structure is much cleaner:
/
- public_html/
- example.com/
- admin/
- config/
- data/
- themes/
- index.php
- example2.com/
- admin/
- config/
- data/
- themes/
- index.php
- .htaccess
- example.com/
This is only one of the many possibilities of rewrite rules. If you desire more information on this complex topic, I can recommend the Apache documentation.