从.htaccess运行PHP代码?

JD *_*cks 7 php apache .htaccess

假设我有一个重写,需要将url传递给PHP函数并检索一个值,以便告诉新的目标应该是什么?有没有办法做到这一点?

谢谢.

更新:

谢谢到目前为止,伙计们......我仍然遇到麻烦,但我会告诉你我的代码:

的.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:$1} [PT]
Run Code Online (Sandbox Code Playgroud)

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.
}
Run Code Online (Sandbox Code Playgroud)

但是我得到500内部服务器错误我不确定我的.htaccess或我的PHP中是否有错误?

Vin*_*vic 11

有一种叫做RewriteMap的东西.

您可以调用一个可执行脚本来返回要重写的URL.

查看本文以获取更多信息和示例(在Perl中,但完全适用于任何其他语言):

http://www.onlamp.com/pub/a/apache/2005/04/28/apacheckbk.html

警告摘要:

  • 必须运行读取STDIN循环(即,在收到URL后不要退出)
  • 必须打印URL以重写为WITH尾随换行符
  • 必须是Apache运行的用户可读和可执行的

这是创建地图的方法

RewriteMap fixurl prg:/usr/local/scripts/fixit.php
Run Code Online (Sandbox Code Playgroud)

现在我们可以在RewriteRule中使用它:

RewriteEngine On
RewriteRule (.*) ${fixurl:$1} 
Run Code Online (Sandbox Code Playgroud)

编辑:关于内部服务器错误.最可能的原因是Gumbo提到的,遗憾的是,RewriteMap不能用于.htaccess.您可以在.htaccess中的RewriteRule中使用它,但只能在服务器配置或虚拟主机配置中创建它.要确定,请检查错误日志.

因此,唯一的PHP/.htaccess解决方案是将所有内容重写为某个PHP程序,该程序使用Location标头进行检查和重定向.就像是:

RewriteRule (.*) proxy.php?args=$1 [QSA]
Run Code Online (Sandbox Code Playgroud)

然后,在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");
?>
Run Code Online (Sandbox Code Playgroud)