How to Rewrite Primary Drupal Domain to Subfolder on cPanel Using .htaccess

Author:
phil
Created:
Thursday, April 04th, 2013
Last Updated:
Thursday, April 04th, 2013

Disclaimer: Accessing the information on this page means you agree to the Sites Terms of Service


So you picked up a domain, setup a shared hosting account with a company using cPanel, and now you want to add a few more addon domains inside of the /public_html folder but alas! You can't get your primary domain running Drupal to link over to the subfolder!

While there are lots of deeper methods to "properly" fixing this, a lot of times you won't have access to these options yourself.

One easy (but costly) method is for you to register a new domain, maybe a .net or something and switch your primary domain over to the new .net and only use the .net for logging into your cpanel, ftp, shell etc. You just won't use it for an actual website. (I prefer this method)

But, if you're like me and run across a client who doesn't have the luxury of a "fluff" domain and they run a bunch of Drupal site in the ~/ folder because they don't know any better and you want to clean things up, it's actually pretty easy BUT, if you don't have all of the pieces together, can be a nightmare...

For the tutorial, I'm going to use example.com as my "Primary" domain. Just change anything that says "example.com" to whatever your domain is.

1) The first thing you will need to do is create your subfolder. I like to name the subfolder as the domain names that hold the files to make things easy to understand.

So create something like:

mkdir /home/[username]/public_html/example.com

2) Next, move all of your files into this directory, including your .htaccess

3) Since we moved the .htaccess file, now we have to create a new one in the /public_html folder

touch /home/[username]/public_html/.htaccess

4) Copy and paste the following into the new .htaccess file:

Options -Indexes

RewriteEngine on

# For security reasons, Option followsymlinks cannot be overridden.
# Options +FollowSymLinks
Options +SymLinksIfOwnerMatch

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^$ subdirectory/index.php [L]
RewriteCond %{HTTP_HOST} ^\example\.com$ [NC]
RewriteRule ^$ subdirectory/index.php [L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subdirectory%{REQUEST_URI} -f
RewriteRule .* subdirectory/$0 [L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* subdirectory/index.php?q=$0 [QSA]

5) Change all of the example.com and subdirectory stuff over to your own environment.

Now that all of the redirect stuff is working, you'll need to edit the .htaccess and settings.php file.

1) Edit the /sites/default/settings.php file and go to where it shows

# $base_url = 'http://www.example.com';  // NO trailing slash!

Change the domain to your own and don't forget to uncomment it.

2) Edit the Drupal .htaccess file and go down to the line where you can set the with WWW or without WWW

Because of the above code, it forces "with" www and you will probably want to comment out the www redirections if they are set. If you want to force the non-www version, edit the first two lines to:

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

That's it! You should be off and running :)

Post Comment