When you're on a shared host like Lunarpages, you'll sometimes need to install drupal in subfolder. Often, you'll want to make it look like the subfolder is the base URL. To do that, edit .htaccess in your domain's public HTML root to redirect requests for pages, as instructed here: http://support.lunarpages.com/knowledge_bases/article/549

You'll need to make sure that the redirect-to-subfolder command in your domain's .htaccess file isn't redundant with the settings.php (in sites/default/) $base_url line. If they're redundant, you'll get an infinite loop during certain tasks.

In sites/default/settings.php, you will have: $base_url = 'http://www.MY-SITE.com'; // NO trailing slash!

DON'T list the base URL as$base_url = 'http://www.MY-SITE.com/subfolder';

You'll also need to add some cookie settings. The defaults won't work in this context.

$cookie_domain = ".www.MY-SITE.com";
ini_set('session.cookie_path', '/subfolder/');

Be aware that there is a danger of cookie data leaking to other sites. See #2515054: [PP-1] Session and other cookies may leak to other apps when Drupal is in a subdirectory for details.

To prevent an infinite redirect loop with sophisticated external connection applications and other errors, let the .htaccess file in the public HTML root (one folder above the subfolder) do the redirecting. Don't include your subfolder in your settings.php Base URL. This may seem obvious, but it is easy to miss because the redundancy doesn't cause problems until you use sophisticated programs like the fb modules.

Comments

Neil’s picture

My site is located off of the web server root in a subfolder called foo. Before following the instructions in this article my site would appear as www.foo.com/foo/somepage. After it would appear as www.foo.com/somepage, which is what is wanted, but when clicking on the login menu item I get :

Index of /user

Name Last modified Size Description
Parent Directory

Basically a directory listing with no files in it. I have checked file ownership/protection and everything appears correct, at least to me.

I am new to Drupal so any help would be appreciated.

Runs on:

CentOS
with Php 5.1.6

Regards
Neil

LupineDream’s picture

These worked for me, including themes, which would not load before, on a symlinked container outside public_html w/ a cpanel subdomain in bluehost:

ensure /sites/default/settings.php points at the full pathname of your subfolder.
ensure apache is confiugred (post vhost conf ) with:

# SPLENDID PUBFILE IDEBUGGED BY:  Bitnami Drupal Stack / WHM - LupineDream
<Directory "./public_hhtml">
    Options -Indexes +FollowSymLinks +ExecCGI
    AllowOverride All
    Require all granted
</Directory>
# SPLENDID FILTER MODULE IDEBUGGED BY:  Bitnami Drupal Stack / WHM - LupineDream
AcceptFilter http none
AcceptFilter https none
<IfModule mod_deflate.c>
          AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
          AddOutputFilterByType DEFLATE application/x-javascript application/javascript application/ecmascript
          AddOutputFilterByType DEFLATE application/rss+xml
</IfModule>

Ensure mod_rewrite is enabled in php.ini
LoadModule rewrite_module modules/mod_rewrite.so

login ssh as root
cd to your drupal container and then:

cd ..
chmod o+x ./drupal/sites/default/files        <- or wherever configured

symlink your outside-root container to your drupal installation:

cd /home/youruser
ln -s /your-drupal-path/ site

Use these instead of lunarsite for cpanel after you go add your subdomain (with a docroot of public_html/site)

# REWRITE RULES FOR DRUPAL CONTAINER:  This gets main.yourdomain.com subdomain
# working for drupal by redirection to the symlinked container.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(main.)?yourdomain.com$
RewriteCond %{REQUEST_URI} !^/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ www.yourdomain.com/site/$1

Should work now. What this does is symlink your container to public_html/site, which inherits all permissions configured for public_html and Options +FollowSymlinks allows the webserver to forward the content from the URL cpanel links to your subdomain to this symlink directly. Additional .htaccess block rules in post vhost httpd.conf allow the symlinks and cgi execution, and block nobody (your visitors) from chaning anything or figuring out you've symlinked.

Works and themes now work with the mod_rewrite condition on a subdomain for me. (tested).

Doing my container outside public_html lets me use composer to lock my dependencies too securly (nice!).

shane2peru’s picture

Ok, I setup drupal in a subfolder we will call drupal inside of public_html folder. I then created a sub.domain.com and pointed it to public_html/drupal
Everything was fine, but I setup the sub.domain through cpanel, and made the document root to be drupal. I built my web site to my liking.

Now I follow these instructions to configure my primary site to be pointed to public_html/drupal following this guide with the lunarpages info, and the redirect works, however my site has no theme! All links seem to work, just no menu's, no blocks, no color, nothing, just words and links.

If I create a redirect with the cpanel it all works fine, except, when I go to the primary domain www.domain.com it looks just like that in the link. AS soon as i click a link, I get the domain.com/drupal <-- trailing folder name included in the url and it doesn't go away.

As specified in this guide I set the settings.php file to have the base URL as it should, without the folder. Any ideas would be greatly appreciated as I have played with this for several hours trying to get something set correctly. I commented out all the Rewrite stuff so my static site is still functioning for the time being, however this is quite annoying for me.

Shane

jonahruh’s picture

Hi Thanks for this post, I followed directions above and checked my settings.php file. My index page shows without the drupal folder, /d6, but when I click on any subpage menu, /d6 is still part of the url. For example mysite.com/d6/about. If if i go up and delete /d6 from the url, the page will still display correctly and the /d6 won't get automatically reinserted into the url. But when i click on a menu item directly the /d6 is there. Do i need to rewrite more than just /index.php?

jonahruh’s picture

Uncomment and put site's domain in baseurl line in settings.php

smerny’s picture

I have a similar but totally opposite issue. I have a directory within Drupal that I don't want Drupal to touch. I am trying to do auto indexing (file directory pages) on this directory and all subdirectories... I've tried Options +Indexes and turning off RewriteEngine but nothing seems to work.

I have a sites/default/files directory, if I try going directly to this directory I get my Drupal headers with a message that says it couldn't find this page (instead of displaying a file listing for this directory). However if I go to something like sites/default/files/someFile.html, it will work as I expect (only showing the files content, no drupal headers). I posted my question here as well: http://drupal.stackexchange.com/questions/149173/drupal-vs-options-indexes

SiL3NC3_SWX’s picture

How can this be achieved with Drupal 8.7 ?
Was trying lots of htaccess code, but since now, without success.
Is there a working solution to have /httpdocs/drupal/ (subfolder) properly redirected as website root www.mydomain.com ?