Short version:
I got a request (no trailing slash)
mydomain.com/slides/sessionOne
How can I figure out in a RewriteCond if '_slides/sessionOne'
exists and is a directory? If so, append a trailing /
(but do not prepend a _
) and redirect [R=301]
.
Long version:
I have a fairly empty webroot:
.htaccess
_slides (dir)
_site (dir)
and a .htaccess that almost does what I want:
RewriteEngine On
RewriteBase /
# directly accessing an _underscore-Folder? – Forbid!
RewriteRule ^_ - [R=403,END]
# AAA
RewriteRule ^slides/(.+)$ _slides/$1 [END]
# prevent _site prepend before _slides
RewriteCond %{REQUEST_URI} !^/_(site|slides)
RewriteRule ^(.*)$ _site/$1 [END]
- any URL under
slides/
will find it's stuff in the_slides
folder - any other url will go and look inside the
_site
folder - direct access to both underscore-folders is prevented
Directory-URLs with trailing slash work fine, just like files: For example slides/presentation1/
will internally find _slides/presentation1/index.html
w/o any visible URL change, as it should.
My trouble: Directory-URLs without trailing slash:
slides/presentation1
will first get (internal, non-visible) rewritten to_slides/presentation1
- since the slash is missing, but there is a directoy of that name, Apache automatically makes a visible 301 redirect, sending me to
_slides/presentation1/
.
But I do not want to reveal the underscore-Folders, and this (correctly) gets caught by my check.
So, I would need at the “AAA” position is in pseudo-code:
URI has no trailing slash
AND there exists a directory either under _slides oder _site?
(peeking for it)
==> make a 301 redirect!
but no underscores prepended, just a trailing slash attached...
Here's my attempt (at the AAA position), but it's just not working:
# directory w/o trailing slash inside _slides/ ?
# ==> attach slash and 301
RewriteCond %{REQUEST_URI} [^/]$
RewriteCond _slides/%{REQUEST_FILENAME} -d [OR]
RewriteCond _site/%{REQUEST_FILENAME} -d
RewriteRule ^ %{REQUEST_URI}/ [R=301,L]