为什么 Apache 的 RewriteRule 会显示本地路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1162871/
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
Why is Apache's RewriteRule revealing local paths?
提问by zildjohn01
I'm trying to use RewriteRules in .htaccesswith relative paths, but Apache seems to want to output the physicalpath instead of the serverpath whenever I try to output a relative path. Absolute and server-root paths work fine. For example:
我试图在.htaccess相对路径中使用 RewriteRules ,但是每当我尝试输出相对路径时,Apache 似乎都想输出物理路径而不是服务器路径。绝对路径和服务器根路径工作正常。例如:
RewriteEngine On
# this works fine, 127.0.0.1/ab redirects to 127.0.0.1/cd
RewriteRule ^ab$ /cd [R]
# this doesn't work... 127.0.0.1/wx redirects to 127.0.0.1/C:/path/to/files/yz
RewriteRule ^wx$ yz [R]
Adding a "RewriteBase /" solves the problem, but it's tedious to add the path to every .htaccess, and it makes it harder to change the directory structure. Is there a reason RewriteBase defaults to the current physicalpath instead of the current URIpath?
加个“ RewriteBase /”就解决了,但是给每个加上路径比较繁琐.htaccess,而且改变目录结构也比较麻烦。RewriteBase 是否有理由默认为当前物理路径而不是当前URI路径?
回答by mkilmanas
For those who happen to arrive here from Google (like me), the short checklist:
对于那些碰巧从谷歌来到这里的人(像我一样),简短的清单:
Make sure you have
RewriteBase /(or any other value - the statement is what is important)If you use redirect (
[R],[R=30x], etc) - make sure the new URI starts with a/and contains a path relative to your domain root(If above didn't help yet) Restart Apache, clear your browser's cache (especially if you have used
[R=301]at some point)
确保您拥有
RewriteBase /(或任何其他价值 - 声明很重要)如果您使用重定向(
[R]、[R=30x]等) - 确保新的 URI 以 a 开头/并包含相对于您的域根目录的路径(如果以上还没有帮助)重新启动 Apache,清除浏览器的缓存(特别是如果您曾经使用
[R=301]过)
That's what saved my day, maybe it will save yours too.
这就是拯救我的一天,也许它也会拯救你的一天。
回答by FWH
It's because of the [R] which means the server will redirect to the new path (so the user's browser will issue a new request with the newly sent uri) instead of translating internally the URI to a local path.
这是因为 [R] 这意味着服务器将重定向到新路径(因此用户的浏览器将使用新发送的 uri 发出新请求),而不是在内部将 URI 转换为本地路径。
In your first RewriteRule, there is an slash in the new path, thus the server doesn't try to translate it to the local path, but in the second rule, there is no slash, this is why it redirects to a complete local path. This explains too why it works with the RewriteBase set.
在您的第一个 RewriteRule 中,新路径中有一个斜杠,因此服务器不会尝试将其转换为本地路径,但在第二个规则中,没有斜杠,这就是它重定向到完整本地路径的原因. 这也解释了为什么它适用于 RewriteBase 集。
Either remove the [R] (you can replace it by a [L] in your case, this avoids the server trying to match other rules once it found a matching one), or add a slash before "yz" in your second RewriteRule.
要么删除 [R](在您的情况下,您可以用 [L] 替换它,这可以避免服务器在找到匹配规则后尝试匹配其他规则),或者在您的第二个 RewriteRule 中的“yz”之前添加一个斜杠。
I'd suggest to simply replace the [R] with a [L]: this way, the user won't see the rewritten path, which is generally what RewriteRules intend to do (mainly for SEO purposes), unless you specifically want to redirect your users to a new URL.
我建议简单地用 [L] 替换 [R]:这样,用户将看不到重写的路径,这通常是 RewriteRules 打算做的(主要用于 SEO 目的),除非您特别想将您的用户重定向到新 URL。
回答by ALH
Try this and tell me the result:
# this doesn't work... 127.0.0.1/wx redirects to 127.0.0.1/C:/path/to/files/yz
试试这个并告诉我结果:
# 这不起作用... 127.0.0.1/wx 重定向到 127.0.0.1/C:/path/to/files/yz
RewriteRule ^wx$ /yz [R]
重写规则 ^wx$ /yz [R]
put / before yz.
将 / 放在 yz 之前。

