apache 从 .htaccess 运行 PHP 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1038648/
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
Run PHP code from .htaccess?
提问by JD Isaacks
Say I have a rewrite that needs to pass the url to a PHP function and retrieve a value in order to tell what the new destination should be? is there a way to do that?
假设我有一个重写,需要将 url 传递给 PHP 函数并检索一个值,以便告诉新目标应该是什么?有没有办法做到这一点?
Thanks.
谢谢。
UPDATE:
更新:
Thanks so far guys..I am still having trouble but i'll show you my code:
到目前为止,谢谢大家..我仍然遇到问题,但我会向您展示我的代码:
.htaccess
.htaccess
#I got the file path by echoing DOCUMENT_ROOT and added the rest.
RewriteMap fixurl prg:/var/www/vhosts/mydomain.com/httpsdocs/domain_prototype/code_base/url_handler.php
RewriteEngine On
RewriteRule (.*) ${fixurl:} [PT]
PHP:
PHP:
set_time_limit(0); # forever program!
$keyboard = fopen("php://stdin","r");
while (1) {
$line = trim(fgets($keyboard));
print "www.google.com\n"; # <-- just test to see if working.
}
However I am getting a 500 Internal Server ErrorI am not sure if there is an error in my .htaccess or in my PHP?
但是,我收到500 Internal Server Error我不确定我的 .htaccess 或 PHP 中是否有错误?
回答by Vinko Vrsalovic
There is something called a RewriteMap.
有一种叫做 RewriteMap 的东西。
You can call an executable script that will return the URL to rewrite to.
您可以调用一个可执行脚本,该脚本将返回要重写的 URL。
Check this article for more information and examples (in Perl, but are totally applicable to any other language):
查看这篇文章以获取更多信息和示例(在 Perl 中,但完全适用于任何其他语言):
http://www.onlamp.com/pub/a/apache/2005/04/28/apacheckbk.html
http://www.onlamp.com/pub/a/apache/2005/04/28/apacheckbk.html
Summary of caveats:
注意事项总结:
- Must run a read STDIN loop (ie, don't exit after receiving an URL)
- Must print the URL to rewrite to WITH a trailing newline
- Must be readable and executable by the user Apache runs as
- 必须运行读取 STDIN 循环(即,在收到 URL 后不要退出)
- 必须使用尾随换行符打印要重写的 URL
- 必须是 Apache 运行的用户可读和可执行的
This is the way to create the map
这是创建地图的方法
RewriteMap fixurl prg:/usr/local/scripts/fixit.php
And now we can use it in a RewriteRule:
现在我们可以在 RewriteRule 中使用它:
RewriteEngine On
RewriteRule (.*) ${fixurl:}
EDIT: About the Internal Server Error. The most probable cause is what Gumbo mentions, RewriteMap cannot be used in .htaccess, sadly. You can use it in a RewriteRule in .htaccess, but can only create it in server config or virtual host config. To be certain, check the error log.
编辑:关于内部服务器错误。最可能的原因是 Gumbo 提到的,遗憾的是 RewriteMap 不能在 .htaccess 中使用。您可以在 .htaccess 中的 RewriteRule 中使用它,但只能在服务器配置或虚拟主机配置中创建它。为了确定,请检查错误日志。
So, the only PHP / .htaccess only solution would be to rewrite everything to a certain PHP program which does the checking and redirects using the Location header. Something like:
因此,唯一的 PHP / .htaccess 唯一解决方案是将所有内容重写为某个 PHP 程序,该程序使用 Location 标头进行检查和重定向。就像是:
RewriteRule (.*) proxy.php?args= [QSA]
Then, in proxy.php
然后,在proxy.php
<?php
$url = get_proper_destination($QUERY_STRING); #args will have the URI path,
#and, via QSA you will have
#the original query string
header("Location: $url");
?>
回答by chaos
Yes; you need a RewriteMap, of the External Rewriting Program variety.
是的; 你需要一个RewriteMap,外部重写程序种类。
回答by DanO
What I would do is pass the URL to the PHP page and then do the final redirect using the PHP headers option.
我要做的是将 URL 传递给 PHP 页面,然后使用 PHP 标头选项进行最终重定向。
header("Location: new-page.php");
If you didn't want it to redirect, you could also do an include of the page you want. I find that this is a little more flexible than using RewriteMap.
如果您不希望它重定向,您还可以包含您想要的页面。我发现这比使用 RewriteMap 更灵活一些。
Hope this helps!
希望这可以帮助!
回答by Gumbo
Note that the RewriteMapdirectivecan only be used in the server configor virtual hostcontext.
请注意,该RewriteMap指令只能在服务器配置或虚拟主机上下文中使用。

