使用 .htaccess 隐藏站点 URL 的子文件夹
Hide a subfolder from site URL using .htaccess
我有这样的 URL 地址 www.example.com/subfolder/index1
我想实现以下结果 www.example.com/index1
(见下文我只有 2 个子文件夹,我必须为两者)。
我项目中的文件夹如下:
index.html (main page)
subfolder1 -> index1.html
subfolder2 -> index2.html
到目前为止,我的 htaccess 文件中有以下代码:
index.html
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
通过拥有两个“隐藏”子目录(/subfolder1
和 /subfolder2
),您造成了歧义。例如。 /foo
的请求应该改写到哪里?应该是 /subfolder1/foo.html
还是 /subfolder2/foo.html
?确定这一点的唯一方法是首先检查/subfolder1
中是否存在文件,否则重写为/subfolder2
.
您似乎也在使用 .html
无扩展名 URL。而且您似乎在引用这两个子文件夹之外的文件。
尝试如下操作:
DirectoryIndex index.html
RewriteEngine On
# Redirect any direct requests for "/subfolder1" or "/subfolder2" back to the root
# - Except for "assets"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !^/[^/]+/assets/
RewriteRule ^(?:subfolder1|subfolder2)(?:$|/(.*)) / [R=301,L]
# Remove "index.html" entirely if requested
RewriteRule (^|.+/)index\.html$ / [R=301,L]
# Remove ".html" extension if requested directly
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^([^.]+)\.html$ / [R=301,L]
# Rewrite requests to subfolder1 if ".html" file exists in that subdir
RewriteCond %{DOCUMENT_ROOT}/subfolder1/.html -f
RewriteRule ^([^.]+)$ subfolder1/.html [L]
# Rewrite requests to subfolder2 if ".html" file exists in that subdir
RewriteCond %{DOCUMENT_ROOT}/subfolder2/.html -f
RewriteRule ^([^.]+)$ subfolder2/.html [L]
# Otherwise append ".html" if the file exists
# - Only required if you are serving other HTML files outside of the subfolders
# - (Excluding "/index.html" in the document root)
RewriteCond %{DOCUMENT_ROOT}/.html -f
RewriteRule ^([^.]+)$ .html [L]
一些假设:
- 你的静态资源(图片、CSS、JS等)直接请求,在URL中包含
/subfolder1
或/subfolder2
。否则,解决 /subfolder1
和 /subfolder2
之间的所有歧义会变得有问题。但是,这确实意味着 /subfolder1
(或 /subfolder2
)并不是真正的“隐藏”。
- 您的无扩展名 URL 不包含点(否则分隔文件扩展名)。
注意:使用 302(临时)重定向进行测试以避免潜在的缓存问题。
我有这样的 URL 地址 www.example.com/subfolder/index1
我想实现以下结果 www.example.com/index1
(见下文我只有 2 个子文件夹,我必须为两者)。
我项目中的文件夹如下:
index.html (main page)
subfolder1 -> index1.html
subfolder2 -> index2.html
到目前为止,我的 htaccess 文件中有以下代码:
index.html
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
通过拥有两个“隐藏”子目录(/subfolder1
和 /subfolder2
),您造成了歧义。例如。 /foo
的请求应该改写到哪里?应该是 /subfolder1/foo.html
还是 /subfolder2/foo.html
?确定这一点的唯一方法是首先检查/subfolder1
中是否存在文件,否则重写为/subfolder2
.
您似乎也在使用 .html
无扩展名 URL。而且您似乎在引用这两个子文件夹之外的文件。
尝试如下操作:
DirectoryIndex index.html
RewriteEngine On
# Redirect any direct requests for "/subfolder1" or "/subfolder2" back to the root
# - Except for "assets"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !^/[^/]+/assets/
RewriteRule ^(?:subfolder1|subfolder2)(?:$|/(.*)) / [R=301,L]
# Remove "index.html" entirely if requested
RewriteRule (^|.+/)index\.html$ / [R=301,L]
# Remove ".html" extension if requested directly
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^([^.]+)\.html$ / [R=301,L]
# Rewrite requests to subfolder1 if ".html" file exists in that subdir
RewriteCond %{DOCUMENT_ROOT}/subfolder1/.html -f
RewriteRule ^([^.]+)$ subfolder1/.html [L]
# Rewrite requests to subfolder2 if ".html" file exists in that subdir
RewriteCond %{DOCUMENT_ROOT}/subfolder2/.html -f
RewriteRule ^([^.]+)$ subfolder2/.html [L]
# Otherwise append ".html" if the file exists
# - Only required if you are serving other HTML files outside of the subfolders
# - (Excluding "/index.html" in the document root)
RewriteCond %{DOCUMENT_ROOT}/.html -f
RewriteRule ^([^.]+)$ .html [L]
一些假设:
- 你的静态资源(图片、CSS、JS等)直接请求,在URL中包含
/subfolder1
或/subfolder2
。否则,解决/subfolder1
和/subfolder2
之间的所有歧义会变得有问题。但是,这确实意味着/subfolder1
(或/subfolder2
)并不是真正的“隐藏”。 - 您的无扩展名 URL 不包含点(否则分隔文件扩展名)。
注意:使用 302(临时)重定向进行测试以避免潜在的缓存问题。