解析子目录中的内容,但保留浏览器中的路径

Resolve content in subdirectory but keep the path in the browser

我的目标是:

我们的网站位于 /web.

的 webroot 中

它包含 .htaccess 文件,但我们想提供来自 /web/content 的内容,但我们不希望用户看到的 URL 只包含 /content他们请求的初始路径。

示例:

用户向URL发出请求:

example.com/color/cool/blue

此请求转到:

/webroot/color/cool/blue (which does not exist)

内容在

/webroot/content/color/cool/blue/index.htm

我们希望用户在浏览器中看到 example.com/color/cool/blue,但看到的内容来自 example.com/content/color/cool/blue/index.htm

我们还希望可以直接访问一些目录,例如:

example.com/exeption/foo.pdf

我们这样做是为了将动态站点转换为静态站点,因此简单地将所有内容移动到根目录或切换 webroot 不是选项。

假设:

  • 目录 file-paths 不包含点。

在根 .htaccess 文件中尝试以下操作:

# 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 (.*)/$ / [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/ -d
RewriteRule ^([^.]+)$ content// [L]

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

在这方面您不一定需要添加任何内容。尽管我假设您指的是“文件”,而不是“目录”。