apache mod_rewrite 到 .htaccess 中的绝对路径 - 出现 404
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2188444/
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
mod_rewrite to absolute path in .htaccess - turning up 404
提问by Pekka
I want to map a number of directories in a URL:
我想在一个 URL 中映射多个目录:
www.example.com/manual
www.example.com/login
to directories outside the web root.
到 Web 根目录之外的目录。
My web root is
我的网络根是
/www/htdocs/customername/site
the manual I want to redirect to is in
我想重定向到的手册在
/www/customer/some_other_dir/manual
In mod_alias, this would be equal to
在 mod_alias 中,这将等于
Alias /manual /www/customer/some_other_dir/manual
but as I have access only to .htaccess, I can't use Alias, so I have to use mod_rewrite.
但由于我只能访问.htaccess,我不能使用别名,所以我必须使用mod_rewrite.
What I have got right now after this questionis the following:
在这个问题之后我现在得到的是以下内容:
RewriteRule ^manual(/(.*))?$ /www/htdocs/customername/manual/ [L]
this works in the sense that requests are recognized and redirected properly, but I get a 404 that looks like this (note the absolute path):
这在请求被正确识别和重定向的意义上起作用,但我得到一个看起来像这样的 404(注意绝对路径):
The requested URL /www/htdocs/customername/manual/resourcename.htm
was not found on this server.
However, I have checked with PHP: echo file_exists(...)and that file definitely exists.
但是,我已经检查过 PHP:echo file_exists(...)并且该文件确实存在。
why would this be? According to the mod_rewrite docs, this is possible, even in a .htaccess file. I understand that when doing mod_rewrite in .htaccess, there will be an automated prefix, but not to absolute paths, will it?
为什么会这样?根据 mod_rewrite 文档,即使在 .htaccess 文件中,这也是可能的。我知道在 .htaccess 中做 mod_rewrite 时,会有一个自动前缀,但不是绝对路径,是吗?
It shouldn't be a rights problem either: It's not in the web root, but within the FTP tree to which only one user, the main FTP account, has access.
它也不应该是权限问题:它不在 Web 根目录中,而是在 FTP 树中,只有一个用户(主 FTP 帐户)可以访问。
I can change the web root in the control panel anytime, but I want this to work the way I described.
我可以随时在控制面板中更改 Web 根目录,但我希望它按照我描述的方式工作。
This is shared hosting, so I have no access to the error logs.
这是共享主机,所以我无法访问错误日志。
I just checked, this is not a wrongful 301 redirection, just an internal rewrite.
我刚刚检查过,这不是错误的 301 重定向,只是内部重写。
回答by Steve Kallestad
In .htaccess, you cannot rewrite to files outside the wwwroot.
在 .htaccess 中,您不能重写 wwwroot 之外的文件。
You need to have a symbolic link within the webroot that points to the location of the manual.
您需要在 webroot 中有一个指向手册位置的符号链接。
Then in your .htaccess you need the line:
然后在您的 .htaccess 中,您需要以下行:
Options +SymLinksIfOwnerMatch
or maybe a little more blindly
或者更盲目一点
Options +FollowSymlinks
Then you can
然后你可以
RewriteRule ^manual(/(.*))?$ /www/htdocs/customername/site/manual/ [L]
where manual under site is a link to /www/customer/some_other_dir/manual
其中,站点下的手册是指向 /www/customer/some_other_dir/manual 的链接
You create the symlink on the command line with:
您可以使用以下命令在命令行上创建符号链接:
ln -s /www/htdocs/customername/site/manual /www/customer/some_other_dir/manual
But I imagine you're on shared hosting without shell access, so look into creating symbolic links within CPanel,Webmin, or whatever your admin interface is. There are php/cgi scripts that do it as well. Of course, you're still limited to the permissions that the host has given you. If they don't allow you to follow symlinks as a policy, you cannot override that within your .htaccess.
但我想你是在没有外壳访问的共享主机上,所以考虑在 CPanel、Webmin 或任何你的管理界面中创建符号链接。也有 php/cgi 脚本可以做到这一点。当然,您仍然受限于主机授予您的权限。如果他们不允许您将符号链接作为策略,则您不能在 .htaccess 中覆盖它。
回答by ziya
AFAIK mod_rewrite works at the 'protocol' level (meaning on the wire HTTP). So I suspect you are getting HTTP 302 with your directory path in the location.
AFAIK mod_rewrite 在“协议”级别(意味着在线 HTTP)工作。因此,我怀疑您正在使用该位置的目录路径获取 HTTP 302。
So I'm afraid you might be stuck unless.. your hosting lets you follow symbolic links; so you can link to that location (assuming you have shell access or this is possible using FTP or your control panel) under your current document root.
所以我担心你可能会被卡住,除非......你的主机允许你遵循符号链接;因此您可以链接到当前文档根目录下的该位置(假设您具有 shell 访问权限,或者可以使用 FTP 或您的控制面板)。
Edit:It actually mentions URL-file phase hook in the docsso now I suspect the directory directivesaren't allowing enough permissions.
编辑:它实际上在文档中提到了URL-file phase hook,所以现在我怀疑目录指令没有允许足够的权限。
回答by BaroqueBobcat
This tells you what you need to know.
The requested URL /www/htdocs/customername/manual/resourcename.htm
was not found on this server.
It interprets RewriteRule ^manual(/(.*))?$ /www/htdocs/customername/manual/$2 [L]to mean rewrite example.com/manual/ as if it were example.com/www/htdocs/customername/manual/.
这会告诉您需要了解的内容。在此服务器上找不到请求的 URL /www/htdocs/customername/manual/resourcename.htm。它解释RewriteRule ^manual(/(.*))?$ /www/htdocs/customername/manual/$2 [L]为重写 example.com/manual/ 就好像它是 example.com/www/htdocs/customername/manual/。
Try
尝试
RewriteRule ^manual(/(.*))?$ /customername/manual/ [L]
instead.
反而。

