RewriteBase 值去哪儿了?

发布于 2025-01-09 08:15:03 字数 1695 浏览 3 评论 0原文

我将从这个答案中获取代码: 使用 htaccess 将文件夹重定向到另一个

所以,我想知道 RewriteBase 真正的去向吗?!根据链接答案中的聪明人的说法,它转到RewriteRule的第一部分。所以第二部分需要斜杠(“/”),

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^test/(.*)$ /new/$1 [L,NC,R=302]

我不记得在哪里,但我在某处读到它指向第二部分。而事实上我的.htaccess就是按照这个规则写的。所以,我的重写规则看起来是这样的

RewriteRule ^script$ folder/some_script [L,NC,R=302]

,实际上它对我有用。但现在我阅读了链接的答案,因为我需要其他东西,而且我很困惑。我不知道这是否会影响性能,我只是想做得正确。我想知道“它真正走向何方”,是第一部分,第二部分,还是两者?现在我需要从文件夹+脚本重定向到另一个文件夹+脚本。那么应该如何呢?

#v1
RewriteBase /
RewriteRule ^/folder/some_script$ other_folder/some_other_script [L,NC,R=302]

#v2
RewriteBase /
RewriteRule ^folder/some_script$ /other_folder/some_other_script [L,NC,R=302]

#v3
RewriteBase /
RewriteRule ^/folder/some_script$ /other_folder/some_other_script [L,NC,R=302]⠀

#v4
RewriteBase /
RewriteRule ^folder/some_script$ other_folder/some_other_script [L,NC,R=302]⠀

我想澄清一下“是”,我需要重定向到另一个文件夹,并且其中有一个具有不同名称的脚本,所以我不需要这个“$1”。还有一件事请不要猜测,如果您不确定,请不要回答。我也可以猜测很多事情,我可以测试这是否有效。我知道当我将斜杠放在第二部分时,它会起作用,如果你不放它也起作用。我的意思是:

RewriteBase /
# this works for me correctly
RewriteRule ^some_script$ folder/some_script [L,NC,R=302]

# this also works for me correctly, but I use the first way
# RewriteRule ^script$ /folder/some_script [L,NC,R=302]

我想知道它是如何正确的!我想知道如何通过文件夹重定向来正确执行此操作。

I'm gonna take a code from this answer:
Redirect folder to another with htaccess

So, I'd like to know where RewriteBase goes for real?! According to the smart guy from the linked answer it goes to the first part of the RewriteRule. So the second part requires a slash ("/")

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^test/(.*)$ /new/$1 [L,NC,R=302]

I don't remember where, but I read somewhere that it goes to the second part. And in fact my .htaccess is written in accordance with this rule. So, my rewrite rules looks this way

RewriteRule ^script$ folder/some_script [L,NC,R=302]

And actually it works for me. But now I read the linked answer because I need something other and I'm confused. I have no idea if this affects performance much, I just want to do it right. And I want to know "where it goes for real", to the first part, to the second, or to the both? Now I need a redirect from the folder+script to another folder+script. So how it should be?

#v1
RewriteBase /
RewriteRule ^/folder/some_script$ other_folder/some_other_script [L,NC,R=302]

#v2
RewriteBase /
RewriteRule ^folder/some_script$ /other_folder/some_other_script [L,NC,R=302]

#v3
RewriteBase /
RewriteRule ^/folder/some_script$ /other_folder/some_other_script [L,NC,R=302]⠀

#v4
RewriteBase /
RewriteRule ^folder/some_script$ other_folder/some_other_script [L,NC,R=302]⠀

I'd like to clarify that "yes", I need a redirect to another folder and which has a script with different name, so I don't need this "$1". One more thing please, don't guess, don't answer if you don't know that for sure. I can guess a lot of things too, I can test if this is work. I know that when I put the slash to the second part, it works, and if you don't put it also works. I mean:

RewriteBase /
# this works for me correctly
RewriteRule ^some_script$ folder/some_script [L,NC,R=302]

# this also works for me correctly, but I use the first way
# RewriteRule ^script$ /folder/some_script [L,NC,R=302]

I want to know how it's right! And I want to know how to do it right with a redirect from a folder.

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

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

发布评论

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

评论(1

养猫人 2025-01-16 08:15:03

如果 RewriteRule 的目标(第二个参数)是相对的,则将 RewriteBase 的值添加到其前面。

因此,我们以 RewriteBase /some/path/ 为例。

  • RewriteRule ^foo$ /bar:RewriteBase 不适用,因为目标 (/bar) 是绝对的
  • RewriteRule ^foo$ http://%{HTTP_HOST} /bar:相同,因为它是到 URL 的 HTTP 重定向,但仍然不是相对路径
  • RewriteRule ^foo$ barRewriteBasebar 起应用 是相对的因此请求 /foo 将被重写为 /some/path/bar

但是,等等,它也可以在没有显式 RewriteBase 的情况下工作吗?是的,它可以:Apache 能够“猜测”默认情况下要在相对目标前面添加的路径,但在某些情况下 Apache 无法正确猜测它(当 Alias 发挥作用或使用 VirtualDocumentRootUserDir 例如)

如果使用相对路径 (R=30[12]) 进行重定向 (而不是URL),Apache 的工作方式相同:它会在其顶部添加 ServerName 和适当的方案。

笔记:

RewriteRule ^/folder/some_script$ ...

在目录上下文中,例如 .htaccess 文件,由于 ^/,这种规则永远不会匹配,因为 Apache 会截断路径以删除与此上下文相对应的路径,包括其最后一个 / (例如:如果 URL 的路径是 /foo,则由 RewriteRule 测试的路径.htaccess位于 DocumentRoot 的文件只是 foo,而不是字符串 /foo)。

If the target (the second argument) of a RewriteRule is relative, then the value of RewriteBase is prepended to it.

So, let's take RewriteBase /some/path/ as example.

  • RewriteRule ^foo$ /bar: RewriteBase doesn't apply since the target (/bar) is absolute
  • RewriteRule ^foo$ http://%{HTTP_HOST}/bar: same because it is an HTTP redirect to an URL, still not a relative path
  • RewriteRule ^foo$ bar: RewriteBase applies since bar is relative so the request /foo will be rewritten to /some/path/bar

But, wait, it also works without an explicit RewriteBase? Yes, it can: Apache is able to "guess" the path to prepend by default to a relative target but there is some cases where Apache isn't able to correctly guess it (when an Alias comes to play or the use of VirtualDocumentRoot, UserDir for examples)

In case of a redirect (R=30[12]) with a relative path (instead of an URL), Apache works the same: it will just then prepend the ServerName and appropriate scheme on top of it.

Note:

RewriteRule ^/folder/some_script$ ...

In a directory context, like a .htaccess file, this kind of rule will never match because of the ^/ since Apache truncates the path to remove the part of the path corresponding to this context including its last / (eg: if the path of the URL is /foo, the path tested by RewriteRule in a .htaccess file located at the DocumentRoot is only foo, not the string /foo).

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