My intention is to serve a website from a subfolder without changing the document root to point at that folder (since I cannot because it is on a shared hosting).
For this purpose I added this to my .htaccess
in the docroot:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L]
</IfModule>
Since this is a WordPress site I have the standard WP .htaccess
in /subfolder/
, that is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
It works well in most cases, but not when I try to use a URL that points at a folder without a trailing slash.
Symptom one:
If I open https://example.com/wp-admin
it redirects me to https://example.com/wp-login.php?redirect_to=https://example.com/subfolder/wp-admin/&reauth=1
, which is wrong, but if I open https://example.com/wp-admin/
(with trailing slash) it redirects me to https://example.com/wp-login.php?redirect_to=https://example.com/wp-admin/&reauth=1
which is good. This internally all comes down to in the first case $_SERVER['REQUEST_URI']
gets the value of: /subfolder/wp-admin/
, but in the second case it is /wp-admin/
. Obviously I want the second to be the case even if I type the URL without a trailing slash.
Symptom two:
But to minimise the components involved in the hope of narrowing down the issue I added a /subfolder/test/index.html
file to the server. Now if I open https://example.com/test
it redirects my browser to https://example.com/subfolder/test/
which is not the desired behaviour, but if I open https://example.com/test/
(note the trailing slash added) the browser ends up on the address https://example.com/test/
, the desired behaviour. The contents of the /subfolder/test/index.html
file is served in both cases.
My goal is indeed how to make it work without trailing slashed URLs, but my primary question is about why is this happening? For example I could try to put in place another redirect to redirect every request matching a folder to a triling-slashed URL variant if not having a trailing slash, probably it'll be my workaround, but that still does not give me the understanding of what is going on and prevent me from similar issues in the future as well.
/route
→/index.php
→/subfolder/index.php
/subfolder/
does not appear in the visible URL, to make it "look like" it is served from the root, since the OP did not explicitly state this in the opening description. Yes, the "double rewrite" is unnecessary (I see you've mentioned this in your answer; as have I) - only issue with manually correcting this is if they are allowing WordPress to manage this part of the.htaccess
file.