WordPress Installed in a Sub-Folder

By MOJO
In Learn
Feb 11th, 2016
0 Comments
1409 Views

By default, WordPress installs to the root folder on you server share. Recently, a client of mine wanted to migrate their WordPress site to the same hosting company (and server) where their CRM is currently installed. They are still a small company (but growing) so the load of having them on the same server won’t be an issue for a long while. I’ll write another post in a bit that explains in detail how I migrated the WordPress site from one host/server to the another.

Anyway, their CRM is installed in a sub-folder of their root (it is also a sub-domain). Since these are both separate ‘applications’, I wanted them to be installed as on the server ‘side-by-side’ instead of the CRM appearing to live within the WordPress application. So I installed WordPress into a sub-folder of the root as well. Because this was the primary domain on their account, I had no way within cPanel to point the primary domain to the new sub-folder. So I had to modify the .htaccess file within the root folder.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$
RewriteRule (.*) http://yourwebsite.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(www.)?yourwebsite.com$ [NC]
RewriteCond %{REQUEST_URI} !^/yourwebsite.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yourwebsite.com/$1

RewriteCond %{HTTP_HOST} ^(www.)?yourwebsite.com$ [NC]
RewriteRule ^(/)?$ yourwebsite.com/index.php [L]

I added their IP address in there just so it would get rewritten to the actual domain name and I remove the ‘www’ if it’s there because I find it so antiquated. Their CRM was already configured as a sub-domain and the URL was crm.yourwebsite.com. So the .htaccess file now points to the sub-folder which is also named yourwebsite.com (but of course you can name it whatever you want just be sure the rewriter uses the correct sub-folder name).

Now this was all working great except when I tried to login to the admin page. I was typing into the address bar (there is no link to the admin page on the site) ‘yourwebsite.com/wp-admin’. This would take me to the login page and I would enter my credentials but I it would keep rejecting me. So I started troubleshooting. I could login to the admin area if I typed ‘yourwebsite.com/wp-login.php’ into the address bar. So I tried ‘yourwebsite.com/wp-admin’ again. I then noticed that WordPress redirects this to ‘yourwebsite.com/wp-login.php’ when not logged into the site. It also appends a redirect URL into the query string to be used after login. It was putting in the query string ‘http%3A%2F%2Fyourwebsite.com%2Fyourwebsite.com%2Fwp-admin%2F’. Ah ha! The redirect URL is repeating the domain name. A little more troubleshooting and I tried ‘yourwebsite.com/wp-admin/’. Notice the trailing slash on the URL. This put the proper redirect URL into the query string and I was able to log in just fine.

So now I need to come up with a way to append the trailing slash onto URLs if the user omits it (and of course not do this if it is requesting a specific file). I modified the .htaccess file within the WordPress sub-folder. I added lines 5 and 6 to my already existing .htaccess file.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Basically this says that if the URL is not requesting a file, then append a forward slash to the end of the URL (in RegEx speak). The WordPress site now behaves as it should with these two changes.