The public_html folder is a place to keep folders of the various domains one has at a web host.
Web hosts usually have the main or primary domain in the root of public_html not in a folder similar to the other addon (additional) domains. This will make the public_html be filled with one sites files and folders in addition to the addon folders for other domains. Creating a mess.
- When backing up the primary domain one has to be sure to not include all the subfolders located in public_html. Moving the primary domain to a subfolder makes backing up easier.
- It looks so much nicer having all the domains located inside their own specific folders. Using public_html only for folders.
I did some research and came across the following method.
In cPanel to go file manager and top right in the settings wheel select to show hidden files.
Create or copy an existing .htaccess file into the public_html root. Remove the existing code and paste in the following code instead.
Replace the word yourmaindomain.com (2 places) with your own primary domain name.
Replace the word subfolder (3 places) with the name of your subfolder.
# .htaccess main domain to subfolder redirect
# 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 main domain.
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
# Change 'subfolder' to be the folder you will use for your main 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 main domain.
RewriteRule ^(.*)$ /subfolder/$1
# Change yourdomain.com to be your main 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.)?yourmaindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
If you already have a web site in your subfolder you want to use for your primary/main site
I had already placed a site into the subfolder paaljoachimromdahl but not a site into the public_html root.
So it came up like this: paaljoachimromdahl.com/paaljoachimromdahl
Change the WordPress URL.
I added the above code into the public_root .htaccess file. Logged into the WordPress site by going to either the main domain or the main domain and /paaljoachimromdahl whichever worked at the time. Then went to the WordPress backend: Settings -> General and changed: WordPress Address (URL) and Site Address (URL) to point to the primary domain (without the subfolder /paaljoachimromdahl). Saved and was automatically logged out. Notice the login url might redirect to the old site name. Adjust it to only domainname.com/wp-login.php and remove everything behind the php.
An option to instead change the url through phpMyAdmin.
An option to logging into the WordPress web site is to go to cPanel -> phpMyAdmin and selected the database in use (if you do not know which database it is then check the web sites wp-config.php file) Then select wp_options and edit the siteURL and Home.
NB!!
I have done this a few times and sometimes it just will not work. I have figured out along the way that I have to refresh the cache. Right click in the browser and select Inspect (Element) then move the cursor to the refresh icon and hold down the mouse button and select Refresh and Empty cache. Hopefully it will work after doing this. If not then close the browser tab and reopen it and refresh it again. It can be a bit stubborn before suddenly working. Btw also close the tab and open it up again.
NB2! The above process might be a lot easier if you have not yet added a web site to your subfolder…:) As you can then prepare the subfolder for a web site that will be added. You will likely not have to change the URL in the database or in the WordPress site.
I also made a video:
Resource used:
westhost.com/knowledgebase/display/WES/Can+I+Point+My+Primary+Domain+To+A+Sub-folder