将请求 / 映射到以显示子文件夹中的内容

map requests for / to to display the content from a sub folder

我有以下工作。如果子文件夹存在,它基本上会从子文件夹中提取内容,而不显示 url 中的子文件夹。子文件夹是 /content

例如,如果收到请求。com/foo/test 并且文件夹 /content/foo/test/ 存在,它将呈现请求的内容 URL。因此 URL 将保持原样,即使内容是从子目录呈现的。

太棒了。如何在不实际更改 url 的情况下将请求 / 映射到 /content 目录中的 index.htm 文件?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
# Remove trailing slash on any URL that is requested directly (excludes rewritten URLs)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*)/$ / [R=301,L]

# If request maps to a directory in "/content" then rewrite and append trailing slash
RewriteCond %{DOCUMENT_ROOT}/content/ -d 
RewriteRule ^([^.]+)$ content// [L]

您可以在之前 包含另一个“无条件”规则,以将请求从/ 重写为content/ - 不需要任何文件系统检查。例如:

# Rewrite root
RewriteRule ^$ content/ [L]

# If request maps to a directory in "/content" then....
: