Home > Blockchain >  Resolve content in subdirectory but keep the path in the browser
Resolve content in subdirectory but keep the path in the browser

Time:04-26

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/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/color/cool/blue in the browser But see the content from what is example/content/color/cool/blue/index.htm

We also would like some directories to be directly accessed like: example/exeption/foo.pdf

We are doing this as a conversion of a dynamic site to a static site so simply moving thing to the root or switching the webroot are not options.

CodePudding user response:

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]

# 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".

  • Related