htaccess 隐藏子目录而不更改基本根目录,并且另一个子目录除外
我有一个网站 https://example.com
,
它有 2 个子目录:https://example.com/admin
和 https://example.com /商店。
- 我想从 URL 中隐藏
/store
这样我的请求将如下所示:https://example.com/shop.php?id=someid
- 但另外,我希望
https://example.com/admin/index.php
能够工作。 - 我找到了一些同时实现 1 和 2 的答案,但它们更改了基本根,因此我所有的 css、js、图像都无法加载
到目前为止,我已经:
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule ^store/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule !^store/ store%{REQUEST_URI} [L]
这实现了 1 但不是 2。
I have a website https://example.com
which has 2 subdirectories, https://example.com/admin
and https://example.com/store
.
- I want to hide
/store
from URL so my requests would look like:https://example.com/shop.php?id=someid
- But Also, I want
https://example.com/admin/index.php
to work. - I found some answers that achieve both 1 and 2 but they change base root so all my css,js, images don't load
So far I have:
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule ^store/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule !^store/ store%{REQUEST_URI} [L]
This achieves 1 but not 2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第二条规则中...排除
RewriteRule
模式中的/admin
子目录(以及/store
)模式。并添加条件以排除包含文件扩展名的请求(即.css
、.js
、.png
等)。例如:或者(尽管效率稍低),排除已映射到物理文件的任何请求:
此规则假设您在
/store
子目录中有另一个.htaccess
文件它还使用重写引擎。 (但如果是这种情况,那么第一条规则实际上不会执行任何操作。)除非您托管多个域/站点,否则您不需要检查请求的
主机
的第一个条件。In the second rule... exclude the
/admin
subdirectory (as well as/store
) in theRewriteRule
pattern. And add a condition to exclude requests that contain a file extension (ie..css
,.js
,.png
, etc.). For example:Alternatively (although marginally less efficient), exclude any request that already maps to a physical file:
This rule assumes you have another
.htaccess
file in the/store
subdirectory that also uses the rewrite engine. (But if that is the case then the first rule isn't actually doing anything.)Unless you are hosting multiple domains/sites then you don't need the first condition that checks the requested
Host
.