RewriteBase 做什么以及如何使用它? [英] What does RewriteBase do and how to use it?

查看:40
本文介绍了RewriteBase 做什么以及如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习一些 .htaccess 技巧.我遇到了 RewriteBase 指令,但无法使其正常工作.

I am trying to learn some .htaccess tricks. I came across the RewriteBase directive but could not get it to work properly.

我想知道这个指令具体做什么以及如何使用它.在 StackOverflow 和 Apache 文档中已经有一些关于 RewriteBase 的讨论,但我仍然无法得到我的问题的明确答案.

I wonder what this directive does specifically and how to use it. There have been some discussions about RewriteBase in StackOverflow and the Apache documentation but still, I could not get a clear answer to my question.

谁能告诉我一个简单但有效的例子,其中可以使用 RewriteBase?

Could someone show me a simple but effective example where RewriteBase can be used?

推荐答案

在 htaccess 文件中,mod_rewrite 的工作方式类似于 容器.并且 RewriteBase 用于提供相对路径基础.

In an htaccess file, mod_rewrite works similar to a <Directory> or <Location> container. and the RewriteBase is used to provide a relative path base.

例如,假设您有以下文件夹结构:

For example, say you have this folder structure:

root
 |-- subdir1
 |-- subdir2
       |-- subsubdir

因此您可以访问:

  • http://example.com/ (root)
  • http://example.com/subdir1 (subdir1)
  • http://example.com/subdir2 (subdir2)
  • http://example.com/subdir2/subsubdir (subsubdir)
  • http://example.com/ (root)
  • http://example.com/subdir1 (subdir1)
  • http://example.com/subdir2 (subdir2)
  • http://example.com/subdir2/subsubdir (subsubdir)

通过 RewriteRule 发送的 URI 是相对于目录的.所以如果你有:

The URI that gets sent through a RewriteRule is relative to the directory. So if you have:

RewriteRule ^(.*)$ - 

在root中,请求为/a/b/c/d,那么捕获的URI($1)为a/b/c/d.但是如果规则在 subdir2 并且请求是 /subdir2/e/f/g 那么捕获的 URI 是 e/f/g.如果规则在 subsubdir 中,并且请求是 /subdir2/subsubdir/x/y/z,那么捕获的 URI 是 x/y/z.规则所在的目录从 URI 中删除了该部分.重写基础对此没有影响,这只是每个目录的工作方式.

in the root, and the request is /a/b/c/d, then the captured URI ($1) is a/b/c/d. But if the rule is in subdir2 and the request is /subdir2/e/f/g then the captured URI is e/f/g. And if the rule is in the subsubdir, and the request is /subdir2/subsubdir/x/y/z, then the captured URI is x/y/z. The directory that the rule is in has that part stripped off of the URI. The rewrite base has no affect on this, this is simply how per-directory works.

重写基础所做的是为规则目标中的任何相对路径提供一个 URL 路径基础(不是文件路径基础).所以说你有这个规则:

What the rewrite base does do, is provide a URL-path base (not a file-path base) for any relative paths in the rule's target. So say you have this rule:

RewriteRule ^foo$ bar.php [L]

bar.php 是相对路径,而不是:

The bar.php is a relative path, as opposed to:

RewriteRule ^foo$ /bar.php [L]

其中 /bar.php 是绝对路径.绝对路径将始终是根"(在上面的目录结构中).这意味着如果规则在root"、subdir1"、subsubdir"等中./bar.php 路径总是映射到 http://example.com/bar.php.

where the /bar.php is an absolute path. The absolute path will always be the "root" (in the directory structure above). That means if the rule is in the "root", "subdir1", "subsubdir", etc. The /bar.php path always maps to http://example.com/bar.php.

但是另一个规则,使用相对路径,它基于规则所在的目录.所以如果

But the other rule, with the relative path, it's based on the directory that the rule is in. So if

RewriteRule ^foo$ bar.php [L]

在根"中,你去http://example.com/foo,你得到服务http://example.com/bar.php.但是,如果该规则位于subdir1"目录中,并且您转到 http://example.com/subdir1/foo,您将获得 http://example.com/subdir1/bar.php.等等.这有时有效,有时无效,正如文档所说,相对路径应该必需,但大多数时候它似乎有效.除非您正在重定向(使用 R 标志,或隐式,因为您的规则目标中有 http://host ).这意味着这个规则:

is in the "root" and you go to http://example.com/foo, you get served http://example.com/bar.php. But if that rule is in the "subdir1" directory, and you go to http://example.com/subdir1/foo, you get served http://example.com/subdir1/bar.php. etc. This sometimes works and sometimes doesn't, as the documentation says, it's supposed to be required for relative paths, but most of the time it seems to work. Except when you are redirecting (using the R flag, or implicitly because you have http://host in your rule's target). That means this rule:

RewriteRule ^foo$ bar.php [L,R]

如果它在subdir2"目录中,而您转到http://example.com/subdir2/foo,mod_rewrite 会将相对路径误认为是文件路径而不是 URL-path 并且由于 R 标志,你最终会被重定向到类似的东西:http://example.com/var/www/localhost/htdocs/subdir1.这显然不是您想要的.

if it's in the "subdir2" directory, and you go to http://example.com/subdir2/foo, mod_rewrite will mistake the relative path as a file-path instead of a URL-path and because of the R flag, you'll end up getting redirected to something like: http://example.com/var/www/localhost/htdocs/subdir1. Which is obviously not what you want.

这是 RewriteBase 的用武之地.该指令告诉 mod_rewrite 将什么附加到每个相对路径的开头.所以如果我有:

This is where RewriteBase comes in. The directive tells mod_rewrite what to append to the beginning of every relative path. So if I have:

RewriteBase /blah/
RewriteRule ^foo$ bar.php [L]

并且该规则在subsubdir"中,转到 http://example.com/subdir2/subsubdir/foo 实际上将为我服务 http://example.com/blah/bar.php.bar.php"被添加到基地的末尾.在实践中,这个例子通常不是你想要的,因为你不能在同一个目录容器或 htaccess 文件中有多个基.

and that rule is in "subsubdir", going to http://example.com/subdir2/subsubdir/foo will actually serve me http://example.com/blah/bar.php. The "bar.php" is added to the end of the base. In practice, this example is usually not what you want, because you can't have multiple bases in the same directory container or htaccess file.

在大多数情况下,它是这样使用的:

In most cases, it's used like this:

RewriteBase /subdir1/
RewriteRule ^foo$ bar.php [L]

这些规则在subdir1"目录中的位置和

where those rules would be in the "subdir1" directory and

RewriteBase /subdir2/subsubdir/
RewriteRule ^foo$ bar.php [L]

将在subsubdir"目录中.

would be in the "subsubdir" directory.

这在一定程度上允许您使规则可移植,因此您可以将它们放在任何目录中,只需要更改基础而不是一堆规则.例如,如果您有:

This partly allows you to make your rules portable, so you can drop them in any directory and only need to change the base instead of a bunch of rules. For example if you had:

RewriteEngine On
RewriteRule ^foo$ /subdir1/bar.php [L]
RewriteRule ^blah1$ /subdir1/blah.php?id=1 [L]
RewriteRule ^blah2$ /subdir1/blah2.php [L]
...

这样去 http://example.com/subdir1/foo 将服务 http://example.com/subdir1/bar.php 等等.和假设您决定将所有这些文件和规则移动到subsubdir"目录.与其将 /subdir1/ 的每个实例都更改为 /subdir2/subsubdir/,您可以只拥有一个基础:

such that going to http://example.com/subdir1/foo will serve http://example.com/subdir1/bar.php etc. And say you decided to move all of those files and rules to the "subsubdir" directory. Instead of changing every instance of /subdir1/ to /subdir2/subsubdir/, you could have just had a base:

RewriteEngine On
RewriteBase /subdir1/
RewriteRule ^foo$ bar.php [L]
RewriteRule ^blah1$ blah.php?id=1 [L]
RewriteRule ^blah2$ blah2.php [L]
...

然后当您需要将这些文件和规则移动到另一个目录时,只需更改基础即可:

And then when you needed to move those files and the rules to another directory, just change the base:

RewriteBase /subdir2/subsubdir/

就是这样.

这篇关于RewriteBase 做什么以及如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆