After several uneasy expereinces with Justhost a few years back I decided to migrate my sites onto Hostgator. For some reason, Justhost was creating databases using some kind of a scandinavian coding instead of UTF-8, so I couldn’t use non-latin alphabet on the sites. But it was a breeze to install a WP site in a subfolder while the URL was pointing at the root. When I moved to Hostgator, I found out that not every hosting account is built equial. I had a very tough time setting up a wordpress site under a subfolder, without it getting messy with the url, eg. if you install your wordpress under www.yourdomain.com/wp/, and then want to get rid of that /wp/, it takes a few steps to get it done right. With several tutorials I tried, I ended up merging steps from a few of them and came up with a solution that works. Finally!

So, here is how you can set up a wordpress site in a subfolder AND keep the URL to look as if the wordpress site is installed in the root directory. Just keep in mind that in the scenario I’m using yoursite.ocm as the domain name, and subfolder as the location where WordPress is installed. Naturally, you will need to substitute these two with your actual domain name and subfolder name.

1. In your WP Dashboard, go to Settings-General, and delete the subfolder name from the Website URL field:

Remove Subfolder from URL

Save the changes and go ahead and clear all cookies and sessions from your active website login. This will make sure you avoid seeing 404 Hostgator error pages.

2. In the Public_HTML folder in Cpanel-File Manager, create a .htaccess file with the following info

# Turn on rewrites.
RewriteEngine on

# Don’t apply to URLs that go to existing files or folders.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Only apply to URLs that aren’t already under /wp.
RewriteCond %{REQUEST_URI} !^/subfolder/

# Rewrite all those to insert /subfolder.
RewriteRule ^(.*)$ /subfolder/$1

# Redirect the root folder.
RewriteCond %{HTTP_HOST} ^(www.)?yoursite.com$
RewriteRule ^(/)?$ yoursite.com/ [L]

Save the .htaccess file. This should give you clear access to www.yoursite.com, but if you try to go to another page or blog post, you end up again on the annoying Hostgator 404 page. To get rid of that, you will need one final step.

3. Copy the index.php into public_html and edit it

Go to the instal directory, eg. yoursite.com/subfolder and copy the index.php file from here into public_html. Then, edit one line like so:

require( dirname( __FILE__ ) . ‘/subfolder/wp-blog-header.php’ );

 

This should give you a clear URL structure and none of your web visitors will know that your wordpress site is actually in a subfolder. Now, the down side is that to log in, you will still need to type in http://yoursite.com/subfolder/wp-admin. But, there is a simple workaround. Install the Redirection plugin, and just create a redirect that takes http://yoursite.com/wp-admin and redirects it to http://yoursite.com/subfolder/wp-admin.

Just as an added note, you may want to keep the login url (wp-admin.php, wp-login.php) hidden or changed, so that hackers will have a harder time to get to the login page of your website. If your website is not secured, and you do want to make it more hacker-proof, get in touch with me using the Contact page.

Like/Share/Comment.

Thanks for reading.