Reputation: 578
Ok. So, I've taken a look at eerily similar questions such as this one and this one, but, while they've definitely helped, I'm still running into one last issue.
Suppose we have the following URLs:
http://playground.dev/projects/project1
http://playground.dev/projects/project1/work
http://playground.dev/projects/project1/work/1.txt
And suppose we have the following directory structure:
/var/www
- projects/
-- project1/
--- public_html/
---- index.html
---- work/
----- 1.txt
My goal is to write a generic .htaccess file that will live within the "root" of each project (project1, project2, etc.), and will map the above URLs to their appropriate files and directories.
Now, I've pretty much figured it out:
RewriteEngine On
RewriteRule ^public_html - [L]
RewriteRule ^(.*)$ public_html/$1
This gets me what I want. The above-mentioned URLs all map like they're supposed to, and most outliers/erroneous input is properly dealt a 4xx; however, there's one glitch: URLs like the following are accepted by Apache.
http://playground.dev/projects/project1/work/1.txt/fgdgfgfdgfdgd
No, fgdgfgfdgfdgd
does not exist, nor is "1.txt" a directory, yet not only does Apache allow this abomination to go on without so much as a 404, but somehow manages to pass through to the "1.txt" file as if the URL I had entered was http://playground.dev/projects/project1/work/1.txt
.
This does not happen when I remove the .htaccess file and try the same thing on the full path. The apache docs are shedding little light, and Google is sputtering. Maybe I'm tired and missing something... but just what is going on here?
Let me be a bit more specific:
The URL is composed like so: http://playground.dev/projects/{projectname}{path}
where {projectname} and {path} are not only both variable (although projectname is only ever a single directory), but {path} may or may not exist, may or may not be 3 directories deep, or may even be 300 directories deep.
Upvotes: 1
Views: 484
Reputation: 11809
To do a global redirect as long as the file or directory exists, you may try this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !public_html [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /public_html/$1 [L,NC]
For permanent and visible redirection, replace [L,NC] with [R=301,L,NC]
Upvotes: 2