为什么 Apache 的 RewriteRule 会显示本地路径? [英] Why is Apache's RewriteRule revealing local paths?
问题描述
我试图在具有相对路径的 .htaccess
中使用 RewriteRules,但 Apache 似乎想要输出 物理 路径而不是 服务器 path 每当我尝试输出相对路径时.绝对路径和服务器根路径工作正常.例如:
I'm trying to use RewriteRules in .htaccess
with relative paths, but Apache seems to want to output the physical path instead of the server path whenever I try to output a relative path. Absolute and server-root paths work fine. For example:
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]
添加一个RewriteBase/
"解决了这个问题,但是为每个.htaccess
添加路径很繁琐,而且更难改变目录结构.是否有原因 RewriteBase 默认为当前 物理 路径而不是当前 URI 路径?
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 physical path instead of the current URI path?
推荐答案
对于那些碰巧从 Google 来到这里的人(像我一样),简短的清单:
For those who happen to arrive here from Google (like me), the short checklist:
确保您有
RewriteBase/
(或任何其他值 - 声明很重要)
Make sure you have
RewriteBase /
(or any other value - the statement is what is important)
如果您使用重定向([R]
、[R=30x]
等) - 确保新 URI 以 /
并包含相对于您的域根目录的路径
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
(如果以上还没有帮助)重新启动 Apache,清除浏览器的缓存(特别是如果您曾经使用过 [R=301]
)
(If above didn't help yet) Restart Apache, clear your browser's cache (especially if you have used [R=301]
at some point)
这就是拯救我的一天,也许它也能拯救你的一天.
That's what saved my day, maybe it will save yours too.
这篇关于为什么 Apache 的 RewriteRule 会显示本地路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!