On Apache 2.4 I have an .htaccess (in a subfolder) which rewrites slashless requests inside that folder to appropriate index files:
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule (.*) $1/index.html [L]
This works for the slashless version exactly as expected. Now I want to redirect the slashed version externally to the slashless version. I tried adding the lines:
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} /$
RewriteRule ^(.*)/ $1 [R=302,L]
However this does not work: The redirect is issued, however it does not go to the slashless URL, but to a URL with a system specific part injected.
So, for a sample URL http://example.com/path/to/dir/
the redirected URL looks like this http://example.com/fs9e/username/sub/public/path/to/dir
instead of just http://example.com/path/to/dir
.
How can I fix this? Many thanks for any pointers!
PS: The real case is a little bit more complicated because I do a subdomain-to-folder rewrite in the root .htacces, but I assume this is not relevant here.
CodePudding user response:
RewriteRule ^(.*)/ $1 [R=302,L]
You are missing the slash prefix (/
) on the substitution string (2nd argument) - to make the substitution root-relative. Or rather, /subfolder/
(since this .htaccess
file is located in a subfolder). Since this is a relative substitution string (not starting with a slash or scheme hostname), the directory-prefix*1 (which I assume is /fs9e/username/sub/public/path/
) is added back (by default*2), resulting in a malformed redirect. (This is correct for internal rewrites, but not external redirects.)
It should be like this:
RewriteRule ^(.*)/$ /subfolder/$1 [R=302,L]
Note you were also missing the end-of-string anchor ($
) on the RewriteRule
pattern. (This also negates the need for the preceding condition that checks that REQUEST_URI
ends in a slash.)
Note also that this "redirect" should go before the earlier "rewrite".
*1 The directory-prefix is the absolute filesystem path of the location of the .htaccess file.
*2 The alternative is to set a RewriteBase /subfolder
- but that then affects all relative substitutions. You could also use an environment variable to apply a specific prefix only to some rules.