动态隐藏所有子文件夹,但初始文件夹

发布于 2025-01-21 04:59:27 字数 467 浏览 1 评论 0原文

从本质上讲,我需要编辑.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2025-01-28 04:59:27

您可以执行以下操作以“隐藏” page/work/file/目录之后以及最后一个路径段之前。

这需要进入root .htaccess文件。

RewriteEngine On

# Redirect "/folder/page/work/<file>" to "/folder/<file>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(folder)/page/work/([^/]+)$ /$1/$2 [R=302,L]

# Rewrite "/folder/<file>" back to "/folder/page/work/<file>" (if it exists)
RewriteCond %{DOCUMENT_ROOT}/$1/page/work/$2 -f
RewriteRule ^(folder)/([^/]+)$ $1/page/work/$2 [L]

将在外部重定向/folder/page/work/assignments.php?something /folder/assignments.php?something。并在内部再次重写请求。

NB:您应该已经在内部链接到/文件夹/&lt; file&gt;。如果您要更改现有的URL结构,则删除页面/工作的初始重定向仅适用于SEO。否则,这不是必需的。


看看您现有的规则...

  rewriteCond%{http_host} ^(www \。)?domain \ .com $
重写!^subfolder / / subfolder%{request_uri} [l]
 

这似乎与您要实现的目标无关。它将内部重写/&lt; nothing&gt; /subfolder/&lt; nothing&gt;的请求,本质上是“隐藏” /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.

RewriteEngine On

# Redirect "/folder/page/work/<file>" to "/folder/<file>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(folder)/page/work/([^/]+)$ /$1/$2 [R=302,L]

# Rewrite "/folder/<file>" back to "/folder/page/work/<file>" (if it exists)
RewriteCond %{DOCUMENT_ROOT}/$1/page/work/$2 -f
RewriteRule ^(folder)/([^/]+)$ $1/page/work/$2 [L]

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 remove page/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...

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L]

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文