【发布时间】:2012-01-11 03:46:03
【问题描述】:
我有一个网站,我正在尝试将 search/ks3/7/example-topic 重写为 search.php?ks=ks3&year=7&topic=example-topic
我正在使用 apache mod_rewrite,我的 .htaccess 看起来像这样:
RewriteEngine On
RewriteBase /
RewriteRule ^search/(a-zA-Z0-9+)/(a-zA-Z0-9+)/(a-zA-Z0-9-+) search.php?ks=$1&year=$2&topic=$3 [R=301,L]
但是,当我浏览到 search/ks3/7/example 时,我的 php $_GET['ks']、$_GET['topic'] 和 $_GET['year'] 不包含任何数据。
我的 apache 错误日志显示 PHP“未定义索引 ks”等错误。
我的重写日志(详细程度设置为 9)显示:
192.168.0.171 - - [08/Jan/2012:13:11:46 +0000] [192.168.0.10/sid#7ff3cc9b7460][rid#7ff3c65f90a0/initial] (3) [perdir /data/shared/chemistry/] add path info postfix: /data/shared/chemistry/search.php -> /data/shared/chemistry/search.php/ks3/7/example-topic
192.168.0.171 - - [08/Jan/2012:13:11:46 +0000] [192.168.0.10/sid#7ff3cc9b7460][rid#7ff3c65f90a0/initial] (3) [perdir /data/shared/chemistry/] strip per-dir prefix: /data/shared/chemistry/search.php/ks3/7/example-topic -> search.php/ks3/7/example-topic
192.168.0.171 - - [08/Jan/2012:13:11:46 +0000] [192.168.0.10/sid#7ff3cc9b7460][rid#7ff3c65f90a0/initial] (3) [perdir /data/shared/chemistry/] applying pattern '^search/(a-zA-Z0-9+)/(a-zA-Z0-9+)/(a-zA-Z0-9-+)$' to uri 'search.php/ks3/7/example-topic'
192.168.0.171 - - [08/Jan/2012:13:11:46 +0000] [192.168.0.10/sid#7ff3cc9b7460][rid#7ff3c65f90a0/initial] (1) [perdir /data/shared/chemistry/] pass through /data/shared/chemistry/search.php
192.168.0.171 - - [08/Jan/2012:13:11:46 +0000] [192.168.0.10/sid#7ff3cc9b7460][rid#7ff3c65f00a0/subreq] (1) [perdir /data/shared/chemistry/] pass through /data/shared/chemistry/ks3
我的 apache 虚拟主机文件看起来像这样
<VirtualHost *:80>
ServerAdmin powerj96@hotmail.co.uk
DocumentRoot /data/shared/chemistry
<Directory /data/shared/chemistry/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Mod_Rewrite 确实有效。我已经尝试了规则:
RewriteRule ^google http://www.google.com [R=301,L]
这行得通。
我还在服务器上运行了4个其他虚拟主机,其中3个只是网站,1个是传输的Web界面。
我正在运行 Ubuntu Server 11.11
编辑:
我认为问题可能与同时运行反向代理有关。例如,如果反向代理将 192.168.0.10:81(我的服务器的地址和端口 81 虚拟主机)更改为传输/网络,那么传输运行在 192.168.0.10/传输/web 的事实,它既是虚拟主机又是在 192.168.0.10/ 运行的站点的子目录(这篇文章中的问题)可能会导致重写模块中的冲突。如果您认为这可能是问题所在,您是否有任何解决方案的想法。
编辑:
这是我单击链接 search/ks3/7/example-topic 时发生的所有事情的完整日志
(1) [perdir /data/shared/chemistry/] pass through /data/shared/chemistry/search.php
(3) [perdir /data/shared/chemistry/] add path info postfix: /data/shared/chemistry/search.php -> /data/shared/chemistry/search.php/ks3/7/example-topic
(3) [perdir /data/shared/chemistry/] strip per-dir prefix: /data/shared/chemistry/search.php/ks3/7/example-topic -> search.php/ks3/7/example-topic
(3) [perdir /data/shared/chemistry/] applying pattern '^search/([^/]+)/([^/]+)/([^/]+)' to uri 'search.php/ks3/7/example-topic'
(1) [perdir /data/shared/chemistry/] pass through /data/shared/chemistry/search.php
(1) [perdir /data/shared/chemistry/] pass through /data/shared/chemistry/ks3
编辑:
查看该日志文件后,我已经意识到问题的根源。由于某种原因,apache 在应用我的重写之前将 search/ks3/7/example-topic 重写为 search.php/ks3/7/example-topic,因此 search/ks3/7/example-topic 不满足
^search/([^/]+)/([^/]+)/([^/]+)
规则。我已将规则更改为
^search.php/([^/]+)/([^/]+)/([^/]+)
这行得通。
您知道如何阻止 apache 将 search/ks3/7/example-topic 重写为 search.php/ks3/7/example-topic 吗?
在移除 QSA、NC 和 R=301 选项后,它现在可以工作了。
【问题讨论】:
标签: mod-rewrite apache2