htaccess 隐藏子目录而不更改基本根目录,并且另一个子目录除外

发布于 2025-01-10 03:52:56 字数 644 浏览 2 评论 0原文

我有一个网站 https://example.com

它有 2 个子目录:https://example.com/adminhttps://example.com /商店。

  1. 我想从 URL 中隐藏 /store 这样我的请求将如下所示: https://example.com/shop.php?id=someid
  2. 但另外,我希望 https://example.com/admin/index.php 能够工作。
  3. 我找到了一些同时实现 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.

  1. I want to hide /store from URL so my requests would look like:
    https://example.com/shop.php?id=someid
  2. But Also, I want https://example.com/admin/index.php to work.
  3. 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 技术交流群。

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

发布评论

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

评论(1

痕至 2025-01-17 03:52:56
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule ^store/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule !^store/store%{REQUEST_URI} [L]

在第二条规则中...排除 RewriteRule 模式中的 /admin 子目录(以及 /store模式。并添加条件以排除包含文件扩展名的请求(即.css.js.png 等)。例如:

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteRule !^(store|admin)($|/) store%{REQUEST_URI} [L]

或者(尽管效率稍低),排除已映射到物理文件的任何请求:

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(store|admin)($|/) store%{REQUEST_URI} [L]

此规则假设您在 /store 子目录中有另一个 .htaccess 文件它还使用重写引擎。 (但如果是这种情况,那么第一条规则实际上不会执行任何操作。)

除非您托管多个域/站点,否则您不需要检查请求的主机的第一个条件。

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule ^store/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule !^store/ store%{REQUEST_URI} [L]

In the second rule... exclude the /admin subdirectory (as well as /store) in the RewriteRule pattern. And add a condition to exclude requests that contain a file extension (ie. .css, .js, .png, etc.). For example:

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteRule !^(store|admin)($|/) store%{REQUEST_URI} [L]

Alternatively (although marginally less efficient), exclude any request that already maps to a physical file:

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(store|admin)($|/) store%{REQUEST_URI} [L]

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.

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