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

查看:342
本文介绍了这是什么的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.

我不知道这个指令不明确,以及如何使用它。已经有大约的RewriteBase在计算器和Apache文档的一些讨论,但我仍然不能得到一个明确的回答我的问题。

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的工作原理类似于&LT;目录&GT; &LT;地点&gt; 容器。而<一href="http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase"><$c$c>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

所以,你可以访问:

So you can access:

  • http://example.com/ (根)
  • 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)

这被通过重写规则是相对于目录发送的URI。所以,如果你有:

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

RewriteRule ^(.*)$ - 

在根,并且请求 / 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 是绝对路径。绝对路径的总是的是根(目录结构同上)。这意味着,如果规则是根,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://例子.COM / bar.php 。但是,如果该规则是在subdir1目录,你去 http://example.com/subdir1/foo ,你得到服务 http://example.com/subdir1/bar.php 。等这有时工作,有时没有,因为文件说,它应该是的需要的相对路径,但大部分似乎工作的时间。除非你重定向(使用研究标志,或隐式,因为你有的http://主机在规则的目标)。这意味着,这条规则:

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的会误以为相对路径文件-path,而不是一个URL路径,并因为研究标志,你将最终获得重定向到类似:的http://例子.COM /无功/网络/本地主机/ 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/

,就是这样。

and that's it.

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

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