apache 我的 RewriteRule 通过 .htaccess 有什么问题?(为什么需要 RewriteBase?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2137492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's wrong with my RewriteRule via .htaccess? (Why does it need RewriteBase?)
提问by user198729
rewriteengine on
rewriterule ^/a/b$ ^/c$
not working,but
不工作,但
rewriteengine on
rewritebase /
rewriterule ^a/b$ ^c$
works.
作品。
回答by outis
It's probably not the RewriteBase that makes the rule work so much as the leading slash. Also, the second argument to RewriteRule isn't a regular expression. Instead, try:
可能不是 RewriteBase 使规则像前导斜杠那样有效。此外, RewriteRule 的第二个参数不是正则表达式。相反,请尝试:
RewriteRule ^/?a/b$ c
When applying a RewriteRule from .htaccess, the leading slash will be stripped from the URL, which will cause the pattern to fail if you include it. By starting a pattern with "^/?", it will work in the main configuration files and in per-directory config files.
从 .htaccess 应用 RewriteRule 时,前导斜杠将从 URL 中删除,如果包含它,这将导致模式失败。通过以“^/?”开头的模式,它将在主配置文件和每个目录的配置文件中工作。
Read over the detailed mod_rewrite documentationfor the details of how the rewrite engine works and the significance of RewriteBase.
阅读详细的 mod_rewrite 文档,了解重写引擎的工作原理和 RewriteBase 的重要性。
Edit: As mentioned in the mod_rewrite technical detailsand described in the documentation for RewriteRuleand RewriteBase, the URL has been translated to a path by the time the per-directory rewrite rules are evaluated. The rewrite engine no longer has a URL to work with. Instead, it removes the local directory prefix (the directory holding the .htaccess file), which ends with a slash. For example, suppose a visitor requests "/var/www/foo/bar/baz.html" and there is a rewrite rule set in "/var/www/foo/.htaccess". Fore each rule, the rewrite engine will strip "/var/www/foo/", leaving "bar/baz.html" to be matched against the rewrite rule. After processing a rule, the local directory prefix is prepended (unless the replacement begins with "http://"). After all the rewriting rules have been processed, the rewrite base, if set, replaces the local directory prefix; if not, the document root is stripped. The rewritten URL is then re-injected as a sub-request.
编辑:如mod_rewrite 技术细节中所述以及RewriteRule和RewriteBase文档中所述,在评估每个目录的重写规则时,URL 已转换为路径。重写引擎不再有可使用的 URL。相反,它会删除以斜杠结尾的本地目录前缀(保存 .htaccess 文件的目录)。例如,假设访问者请求“/var/www/foo/bar/baz.html”并且在“/var/www/foo/.htaccess”中设置了重写规则。对于每个规则,重写引擎将删除“/var/www/foo/”,留下“bar/baz.html”以与重写规则匹配。处理规则后,本地目录前缀将被添加(除非替换以“http://”开头)。处理完所有的重写规则后,如果设置了重写基,则替换本地目录前缀;如果不是,则删除文档根目录。
回答by Abel
What version of Apache are you using? RewriteBaseshould not be necessary when you are rewriting from the root. If you are not, you may need it. For instance, a part of my current configurations (Apache 2.2) for one of my blogs looks as follows, and works:
你用的是什么版本的Apache?RewriteBase当您从根重写时,应该没有必要。如果你不是,你可能需要它。例如,我的博客之一的当前配置(Apache 2.2)的一部分如下所示,并且有效:
RewriteEngine On
RewriteRule ^/$ /blog/ [R]
RewriteRule ^/wordpress/(.*) /blog/ [R]

