在重写期间不解释php变量

时间:2016-02-22 12:43:19

标签: php apache .htaccess mod-rewrite url-rewriting

我的任务是帮助为面向外部的网站提供基础设施,事情基本上很好,它的部署和傀儡控制和我的标准等等。

然而,它变得棘手,因为外部开发人员为我们提供了网站代码,他们很快注意到'重写不起作用' 我花了一些时间检查这个并使用.htaccess文件重写apache确实工作得很好。 它倒下的地方是在重写操作期间没有传递或解释php变量 我发布了一个以前的stackoverflow问题,其中包含非常详细的信息,从那以后我的示例大大简化了。

My mod_rewrite rules do not resolve PHP variables

没有人能够在我之前的帖子中确定问题,所以我简化了我的案例以进行测试。

我的基本故障排除.htaccess文件在这里

<ifModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^test/test2/$ test.php?var=test2&var2=test [L]
RewriteRule ^hello.html redir.html [L]
</ifModule>
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>

并在访问时 http://server.example.net/hello.html 我正确地被带到了我的redir.html页面。所以我知道重写工作,我通过拖尾日志文件进行检查以确认。 现在问题是,当我运行以下

http://server.example.net/test/test2/

它应该重定向到我的test.php文件

http - // server.example.net/test.php?var=test2&var2=test这是一个非常基本的文件

<?php
echo $_GET['var'].'aa'.$_GET['var2'];
?>

现在,如果我在不使用重写的情况下浏览到该位置,我将获得如下的html页面

test2aatest

显示正在解析并显示在页面上的2个变量。但是,如果我访问使用重写的网址

HTTP - // server.example.net/test/test2 /

然后我得到以下

Notice: Undefined index: var in /var/www/itnews/test.php on line 4
Notice: Undefined index: var2 in /var/www/itnews/test.php on line 4
aa

告诉我我已正确重定向到页面,但变量有问题。 如果它有帮助 操作系统 - Centos 7 x64和所有最新更新

php packages
php.x86_64                       5.4.16-36.el7_1                       @base
php-cli.x86_64                   5.4.16-36.el7_1                       @base
php-common.x86_64                5.4.16-36.el7_1                       @base
php-gd.x86_64                    5.4.16-36.el7_1                       @base
php-mbstring.x86_64              5.4.16-36.el7_1                       @base
php-mysql.x86_64                 5.4.16-36.el7_1                       @base
php-pdo.x86_64                   5.4.16-36.el7_1                       @base
php-snmp.x86_64                  5.4.16-36.el7_1                       @base

阿帕奇

httpd.x86_64                     2.4.6-40.el7.centos                   @base
httpd-tools.x86_64               2.4.6-40.el7.centos                   @base

最后,我的重写日志。

[Mon Feb 22 13:10:16.901115 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] add path info postfix: /var/www/itnews/test.php -> /var/www/itnews/test.php/test2/
[Mon Feb 22 13:10:16.901131 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/test.php/test2/ -> test.php/test2/
[Mon Feb 22 13:10:16.901135 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] applying pattern '^test/test2/$' to uri 'test.php/test2/'
[Mon Feb 22 13:10:16.901140 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] add path info postfix: /var/www/itnews/test.php -> /var/www/itnews/test.php/test2/
[Mon Feb 22 13:10:16.901144 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/test.php/test2/ -> test.php/test2/
[Mon Feb 22 13:10:16.901147 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] applying pattern '^hello.html' to uri 'test.php/test2/'
[Mon Feb 22 13:10:16.901156 2016] [rewrite:trace1] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffef2d0/subreq] [perdir /var/www/itnews/] pass through /var/www/itnews/test.php
[Mon Feb 22 13:10:16.901241 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] add path info postfix: /var/www/itnews/test.php -> /var/www/itnews/test.php/test2/
[Mon Feb 22 13:10:16.901251 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/test.php/test2/ -> test.php/test2/
[Mon Feb 22 13:10:16.901255 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] applying pattern '^test/test2/$' to uri 'test.php/test2/'
[Mon Feb 22 13:10:16.901274 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] add path info postfix: /var/www/itnews/test.php -> /var/www/itnews/test.php/test2/
[Mon Feb 22 13:10:16.901279 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/test.php/test2/ -> test.php/test2/
[Mon Feb 22 13:10:16.901282 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] applying pattern '^hello.html' to uri 'test.php/test2/'
[Mon Feb 22 13:10:16.901286 2016] [rewrite:trace1] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffdd240/initial] [perdir /var/www/itnews/] pass through /var/www/itnews/test.php
[Mon Feb 22 13:10:16.901375 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] add path info postfix: /var/www/itnews/test2 -> /var/www/itnews/test2/
[Mon Feb 22 13:10:16.901387 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/test2/ -> test2/
[Mon Feb 22 13:10:16.901391 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] applying pattern '^test/test2/$' to uri 'test2/'
[Mon Feb 22 13:10:16.901395 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] add path info postfix: /var/www/itnews/test2 -> /var/www/itnews/test2/
[Mon Feb 22 13:10:16.901401 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] strip per-dir prefix: /var/www/itnews/test2/ -> test2/
[Mon Feb 22 13:10:16.901405 2016] [rewrite:trace3] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] applying pattern '^hello.html' to uri 'test2/'
[Mon Feb 22 13:10:16.901408 2016] [rewrite:trace1] [pid 29768] mod_rewrite.c(468): [client 192.168.0.55:65280] 192.168.0.55 - - [server.example.net/sid#7f8f6fceb9c0][rid#7f8f6ffe7290/subreq] [perdir /var/www/itnews/] pass through /var/www/itnews/test2

请注意,我在操作系统中并且我不熟悉php,因此我正在研究有关php如何正常工作的第二手信息。

1 个答案:

答案 0 :(得分:0)

写下面的行

RewriteRule ^(.*)/(.*)$ /test.php?var=$1&var2=$2 [NC,L]

而不是

RewriteRule ^test/test2/$ test.php?var=test2&var2=test [L] // incorrect

希望它会对你有所帮助:)。