我有一个网站,我试图将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]
然而,当我浏览搜索/ ks3 / 7 /例如我的php $ _GET [' ks'],$ _GET [' topic']和$ _GET [' ;年份#39;]不包含任何数据。
我的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个是传输的网络界面。
我正在运行Ubuntu Server 11.11
编辑:
我认为问题可能与运行反向代理一起。例如,如果反向代理正在将192.168.0.10:81(我的服务器和端口81 vhost的地址)更改为传输/ web,那么传输正在192.168.0.10/transmission/web运行,这既是vhost又是运行在192.168.0.10/(此帖子中有问题的站点)的站点的子目录可能会导致重写模块发生冲突。如果您认为可能是问题,您对此有任何解决方案吗?
编辑:
这是我点击链接搜索/ 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,因此搜索/ ks3 / 7 / example-topic不满足
^search/([^/]+)/([^/]+)/([^/]+)
规则。我已将规则更改为
^search.php/([^/]+)/([^/]+)/([^/]+)
这可行。
您是否知道如何停止apache重写search / ks3 / 7 / example-topic到search.php / ks3 / 7 / example-topic?
在删除QSA,NC和R = 301选项后,它现在正在运行。
答案 0 :(得分:0)
你的重写并不多见。一开始就删除IP信息(谁在乎?)。并添加重写的开头(=告诉我们将要重写的uri是什么):看一下以下日志的第一行。
这是一个更有用的例子:
(2) init rewrite engine with requested uri /robots.txt
(3) applying pattern '(.*)setup.php' to uri '/robots.txt'
(3) applying pattern '(.*)admin(.*)' to uri '/robots.txt'
(3) applying pattern '(.*)' to uri '/robots.txt'
(4) RewriteCond: input='www.papdevis.com' pattern='!(papdevis)' [NC] => not-matched
(3) applying pattern '(.*)' to uri '/robots.txt'
(4) RewriteCond: input='www.papdevis.com' pattern='(.*)\.(com|net|org|eu)' [NC] => matched
(2) rewrite '/robots.txt' -> 'http://www.papdevis.fr/robots.txt'
(2) explicitly forcing redirect with http://www.papdevis.fr/robots.txt
(1) escaping http://www.papdevis.fr/robots.txt for redirect
(1) redirect to http://www.papdevis.fr/robots.txt [REDIRECT/301]
(2) init rewrite engine with requested uri /
(3) applying pattern '(.*)setup.php' to uri '/'
(3) applying pattern '(.*)admin(.*)' to uri '/'
(3) applying pattern '(.*)' to uri '/'
(4) RewriteCond: input='www.papdevis.com' pattern='!(papdevis)' [NC] => not-matched
(3) applying pattern '(.*)' to uri '/'
(4) RewriteCond: input='www.papdevis.com' pattern='(.*)\.(com|net|org|eu)' [NC] => matched
(2) rewrite '/' -> 'http://www.papdevis.fr/'
(2) explicitly forcing redirect with http://www.papdevis.fr/
(1) escaping http://www.papdevis.fr/ for redirect
(1) redirect to http://www.papdevis.fr/ [REDIRECT/301]
改写自:
search/ks3/7/example-topic
到
search.php?ks=ks3&year=7&topic=example-topic
您的.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]
它永远不会有用......试试这个:
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 [QSA,NC,R=301,L]
...或者也许如何把所有但斜线?这可能看起来像这样:
RewriteEngine On
RewriteBase /
RewriteRule ^search/([^/]+)/([^/]+)/([^/]+) \
search.php?ks=$1&year=$2&topic=$3 [QSA,NC,R=301,L]
请告诉我这是否有效。
如果您不在托管环境中(=如果它是您自己的服务器并且您可以修改虚拟主机,而不仅仅是.htaccess
文件),请尝试使用RewriteLog
指令:它帮助您追踪此类问题:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
我最喜欢检查regexp的工具:
http://www.quanetic.com/Regex(别忘了选择ereg(POSIX)而不是preg(PCRE)!)