'Resolve content in subdirectory but keep the path in the browser
My goal is this:
We have site that lives in a webroot which is /web
.
It contains the .htaccess
file but we want to serve up the content from /web/content
but we do not want the URL the user sees to contain /content
just the initial path they requested.
Example:
The user makes a request to a URL:
example.com/color/cool/blue
This request goes to:
/webroot/color/cool/blue (which does not exist)
The content is in
/webroot/content/color/cool/blue/index.htm
We would like the user to see example.com/color/cool/blue
in the browser, but see the content from what is example.com/content/color/cool/blue/index.htm
.
We also would like some directories to be directly accessed like:
example.com/exeption/foo.pdf
We are doing this as a conversion of a dynamic site to a static site so simply moving everything to the root or switching the webroot are not options.
Solution 1:[1]
Assumptions:
- Directory file-paths do not contain dots.
In the root .htaccess
file try the following:
# Disable directory listings (mod_autoindex) since "DirectorySlash Off"
Options -Indexes -MultiViews
# Prevent trailing slash being appended to directories
DirectorySlash Off
# File to serve from requested directory
DirectoryIndex index.htm
RewriteEngine On
# Remove trailing slash on any URL that is requested directly (excludes rewritten URLs)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*)/$ /$1 [R=301,L]
# Rewrite root
RewriteRule ^$ content/ [L]
# If request maps to a directory in "/content" then rewrite and append trailing slash
RewriteCond %{DOCUMENT_ROOT}/content/$1 -d
RewriteRule ^([^.]+)$ content/$1/ [L]
We also would like some directories to be directly accessed like:
example/exeption/foo.pdf
You don't necessarily need to add anything in this respect. Although I'm assuming you mean "files", not "directories".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |