Search code examples
apache.htaccessdrupal-7

Running Drupal in subfolder - stop access to URL example.com/subfolder/index.php


For various reasons regarding this site's host, we are running a Drupal 7 site inside a subfolder "live" so the path to D7 is /home/username/public_html/live/

To get the Drupal 7 site to load from the subfolder, we have the .htaccess below in /home/username/public_html and it works great.

RewriteEngine on
RewriteRule ^$ live/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/live%{REQUEST_URI} -f
RewriteRule .* live/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* live/index.php?q=$0 [L,QSA]

However, if I type https://website.com/live/index.php, the home page of the site loads and the URL in the location bar does not mask to https://website.com. A previous problem with a different rewrite rule caused other visitors to have reached this page and this has caused https://website.com/live/index.php to be included in our Google Analytics statistics.

Is there a way to rewrite .htaccess rules to avoid https://website.com/live/index.php being visible from now on? The .htaccess at /home/username/public_html/live is the default D7 .htaccess.


Solution

  • Answer was to add another .htaccess file inside the /live folder:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s+\/live/index\.php\s+ [NC]
        RewriteRule ^ https://%{HTTP_HOST} [L,R=301]
    </IfModule>