.htaccess 目录到文件重定向不适用于正斜杠

发布于 2024-12-12 03:01:08 字数 813 浏览 0 评论 0原文

我需要在 .htacess 文件中进行基本重定向,从目录 URL 到文件:

RewriteRule ^(.*)/mydirectory/(.*)$ myfile.php?q=$2

它不起作用。问题是我无法让它工作,除非我这样做:

RewriteRule (.*)mydirectory(.*)$ myfile.php?q=$2

在这种情况下,捕获的字符串($2)是“mydirectory”(?!),当然该规则将适用于包含“mydirectory”作为子字符串的任何URL ,即使它不在斜杠之间,这也不好。

测试 URL 是:

http://localhost/base/mydirectory/some-other-string

我期望的是:

http://localhost/base/myfile.php?q=some-other-string"

第一条规则得到的是什么(规则不适用/不起作用),第二条规则分别得到“myfile.php?q=base/mydirectory”。

如果我使用 RewriteCond,也会发生同样的情况:一旦我添加了该死的正斜杠,该规则就不再适用。我什至逃避了正斜杠,尽管没有必要,但无济于事。

我不是 mod_rewrite 专家,但对它并不陌生。 IMO,第一条规则应该有效,但事实并非如此。我可能正在做一些非常错误的事情,但我离得太近而看不到它。

请看一下,这让我发疯。

非常感谢。

I need a basic redirect in a .htacess file, from a directory URL to a file:

RewriteRule ^(.*)/mydirectory/(.*)$ myfile.php?q=$2

It is not working. The problem is I can't get it to work unless I do:

RewriteRule (.*)mydirectory(.*)$ myfile.php?q=$2

in which case the captured string ($2) is "mydirectory" (?!), and of course the rule would apply to any URL containing "mydirectory" as a substring, even if it's not between slashes, which is no good.

The test URL is:

http://localhost/base/mydirectory/some-other-string

What I expect is:

http://localhost/base/myfile.php?q=some-other-string"

What I get with the first rule is nothing (rule does not apply/not working), and "myfile.php?q=base/mydirectory" with the second rule, respectively.

The same happens if I use RewriteCond: as soon as I add the damned forward slashes, the rule does not apply anymore. I even escaped the forward slashes, although there's no need for that, to no avail.

I'm not a mod_rewrite expert, but I'm no stranger to it. IMO, the first rule should work, but it doesn't. I'm probably doing something horribly wrong, but I'm too close to see it.

Please take a look, it's driving me nuts.

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

聽兲甴掵 2024-12-19 03:01:08

斜杠在正则表达式中具有特殊含义,您需要对其进行转义:

RewriteRule ^(.*)\/mydirectory\/(.*)$ myfile.php?q=$2

Slashes have special meanings in regular expressions you need to escape them:

RewriteRule ^(.*)\/mydirectory\/(.*)$ myfile.php?q=$2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文