Setting Up WP In Sub-Folder That Points To Root On Hostgator Account

by | Apr 23, 2014

how to set up a folder in sub folder

After several uneasy experiences 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 equally. 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:

how to install wordpress in subfolder

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 install 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 downside is that to log in, you will still need to type in https://yoursite.com/subfolder/wp-admin. But, there is a simple workaround. Install the Redirection plugin, and just create a redirect that takes https://yoursite.com/wp-admin and redirects it to https://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.

by | Apr 23, 2014

Recent Posts

How To Write Product Descriptions That Sell

How To Write Product Descriptions That Sell

With the unstoppable rise of ecommerce, and the growing reliance of websites to find, compare, shortlist and buy products, ecommerce owners are facing a complex challenge: How do we replace the senses of touch, smell, sight and taste with written words? How do you...

Categories