0

We have drupal 8 site with a folder in docroot. Lets say its in a folder called micrositefolder. It contains a single index.html file.

Now let's say micrositefolder lives on fullsite.com. I dont want someone to access the microsite via fullsite.com/micrositefolder, but instead only accessible via mymicrosite.com

I have already achieved that with the following:

# Prevent access to the static site from non-static site hosts.
RewriteCond %{REQUEST_URI} ^/micrositefolder [NC]
RewriteCond %{HTTP_HOST} !^mymiscrosite
RewriteRule .* /index.php [L,R=301]

# Only serve the static site if host begins with mymiscrosite.
RewriteCond %{HTTP_HOST} ^mymiscrosite

# Don't loop anything targeting the actual mask directory, to allow
# for linked scripts, stylesheets etc in the static HTML
RewriteCond %{REQUEST_URI} !^/micrositefolder/

#Any requests that made it this far are served from the /micrositefolder/ directory
RewriteRule ^(.*)$ /micrositefolder/$1 [PT]

That works great. I can now visit mymicrosite.com and it serves me that index.html in that folder.

I now have to include another page on that microsite. The url would be mymicrosite.com/ronnie. I created a folder inside of micrositefolder called ronnie with another index.html in it.

When I try to go to that url (mymicrosite.com/ronnie) it is being rewritten to mymicrosite.com/micrositefolder/ronnie/ and I cannot figure out why. I am pretty sure it has to do with that last line in my code snippet, but I cannot figure out how to make it just be mymicrosite.com/ronnie

One thing to note is if I view the url via mymicrosite.com/ronnie/ it works, but if I dont include the slash at the end it redirects to mymicrosite.com/micrositefolder/ronnie

anubhava
  • 761,203
  • 64
  • 569
  • 643
Ronnie
  • 11,138
  • 21
  • 78
  • 140
  • Try your URL with trailing slash to make `mymicrosite.com/ronnie/` after clearing browser cache – anubhava Jul 09 '20 at 06:31
  • @anubhava yes I tried that and it does work, but thats not the problem. The problem is if I dont include the trailing slash it turns into `mymicrosite.com/micrositefolder/ronnie` – Ronnie Jul 09 '20 at 15:55

2 Answers2

1

You can add this rule below your existing rules in site root .htacess:

# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L]

This will add a trailing slash if current request is pointing to a directory.

Problem with your proposed approach (in the answer) is that:

  • It will perform a trailing slash 301 redirect even if it is an invalid URI such as mymicrosite.com/qwerty111
  • For cases like mymicrosite.com/ronnie where /micrositefolder/ronnie is an actual directory, it will perform an extra 301 redirect before showing index.html
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • unfortunately, this is still resulting in `mymicrosite.com/micrositefolder/ronnie` when I input `mymicrosite.com/ronnie` – Ronnie Jul 09 '20 at 18:48
  • 1
    No that's not the behavior I am getting. Did you delete `micrositefolder/.htaccess` and cleared browser cache? – anubhava Jul 09 '20 at 18:50
  • I didn't delete, just commented everything out. The problem probably resides in something else going on in the main .htaccess. The default .htaccess with drupal comes with a bunch of other stuff in it. https://git.drupalcode.org/project/drupal/-/blob/9.0.x/.htaccess – Ronnie Jul 09 '20 at 20:06
  • I think if you remove `micrositefolder/.htaccess` it may work. I am checking Drupal .htaccess as well – anubhava Jul 09 '20 at 20:07
0

Adding an .htaccess inside the micrositefolder with the below seems to have solved my issue

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
</IfModule>
Ronnie
  • 11,138
  • 21
  • 78
  • 140