动态隐藏所有子文件夹,但初始文件夹
从本质上讲,我需要编辑.htaccess
文件,以开发一种在/file
之后隐藏文件夹结构的方法。例如,如果我有此URL:
https://www.example.com/file/page/work/assignments.php?
我只应该看到 https://www.example.com/file/assignments.php?
如何完成?
到目前为止,我已经尝试了以下操作,但是它确实是在第一次隐藏所有文件夹:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L]
Essentially, I need to edit the .htaccess
file to develop a way to hide folder structure after /file
. For example, if I have this URL:
https://www.example.com/file/page/work/assignments.php?
I should only see https://www.example.com/file/assignments.php?
How can this be accomplished?
So far I have tried the following, but it does hide all folders after first:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行以下操作以“隐藏”
page/work
在/file/
目录之后以及最后一个路径段之前。这需要进入root
.htaccess
文件。将在外部重定向
/folder/page/work/assignments.php?something
/folder/assignments.php?something
。并在内部再次重写请求。NB:您应该已经在内部链接到
/文件夹/< file>
。如果您要更改现有的URL结构,则删除页面/工作
的初始重定向仅适用于SEO。否则,这不是必需的。看看您现有的规则...
这似乎与您要实现的目标无关。它将内部重写
/< nothing>
/subfolder/< nothing>
的请求,本质上是“隐藏”/subfolder
。除非您接受多个域的请求,否则无需检查请求的
主机
标题,这仅是这些域之一。 (您在问题中没有提及这一点。)You can do something like the following to "hide"
page/work
after the/file/
directory and before the last path segment.This needs to go in the root
.htaccess
file.The will externally redirect a request for
/folder/page/work/assignments.php?something
to/folder/assignments.php?something
. And internally rewrite the request back again.NB: You should already be linking to
/folder/<file>
internally. The initial redirect to removepage/work
is just for SEO if you are changing an existing URL structure. Otherwise, this is not strictly necessary.A look at your existing rule...
This would seem to be unrelated to what you are trying to achieve. It would internally rewrite a request for
/<anything>
to/subfolder/<anything>
, essentially "hiding"/subfolder
.There is no need to check the requested
Host
header unless you are accepting requests to multiple domains and this is specific to just one of those domains. (You've made no mention of this in your question.)