mod_rewrite 不附加查询
我有一个网站,我试图将 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 [email protected]
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 个是 Transmission 的 Web 界面。
我正在运行 Ubuntu Server 11.11
编辑:
我认为问题可能与同时运行反向代理有关。例如,如果反向代理将 192.168.0.10:81(我的服务器地址和端口 81 虚拟主机)更改为 Transmission/Web,则传输运行在 192.168.0.10/transmission/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 选项后,它现在可以正常工作。
I have a site where I am trying to rewrite search/ks3/7/example-topic to search.php?ks=ks3&year=7&topic=example-topic
I am using apache mod_rewrite and my .htaccess looks like this:
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]
However, when I browse to search/ks3/7/example my php $_GET['ks'], $_GET['topic'] and $_GET['year'] contain no data.
My apache error log shows errors in the PHP "undefined index ks" etc.
My rewrite log (which is set to a verbosity of 9) shows:
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-+)
and my apache virtual hosts file looks like this
<VirtualHost *:80>
ServerAdmin [email protected]
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 is definitely working. I have tried the rule:
RewriteRule ^google http://www.google.com [R=301,L]
and this works.
I also run 4 other virtual hosts on the server, 3 of which are just websites and 1 is transmission's web interface.
I am running Ubuntu Server 11.11
EDIT:
I think the problem may be to do with running reverse proxy alongside this. for example, if reverse proxy is changing 192.168.0.10:81 (address of my server and port 81 vhost) to transmission/web then the fact that transmission is running at 192.168.0.10/transmission/web, which is both a vhost and a subdirectory of the site which is running at 192.168.0.10/ (the one in question on this post), may be causing conflicts in the rewrite module. Do you have any ideas of a solution to this if you think it may be the problem.
EDIT:
This is the complete log for everything that happens when I click the link 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
EDIT:
I have realised the source of the problem after looking at that log file. For some reason apache is rewriting search/ks3/7/example-topic to search.php/ks3/7/example-topic before it applies my rewrite, therefore search/ks3/7/example-topic doesn't satisfy the
^search/([^/]+)/([^/]+)/([^/]+)
rule. I have changed the rule to
^search.php/([^/]+)/([^/]+)/([^/]+)
and this works.
Do you have any idea of how to stop apache rewriting search/ks3/7/example-topic to search.php/ks3/7/example-topic ?
It is working now, after removing the QSA, NC and R=301 options.
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
and my apache virtual hosts file looks like this
Mod_Rewrite is definitely working. I have tried the rule:
and this works.
I also run 4 other virtual hosts on the server, 3 of which are just websites and 1 is transmission's web interface.
I am running Ubuntu Server 11.11
EDIT:
I think the problem may be to do with running reverse proxy alongside this. for example, if reverse proxy is changing 192.168.0.10:81 (address of my server and port 81 vhost) to transmission/web then the fact that transmission is running at 192.168.0.10/transmission/web, which is both a vhost and a subdirectory of the site which is running at 192.168.0.10/ (the one in question on this post), may be causing conflicts in the rewrite module. Do you have any ideas of a solution to this if you think it may be the problem.
EDIT:
This is the complete log for everything that happens when I click the link search/ks3/7/example-topic
EDIT:
I have realised the source of the problem after looking at that log file. For some reason apache is rewriting search/ks3/7/example-topic to search.php/ks3/7/example-topic before it applies my rewrite, therefore search/ks3/7/example-topic doesn't satisfy the
rule. I have changed the rule to
and this works.
Do you have any idea of how to stop apache rewriting search/ks3/7/example-topic to search.php/ks3/7/example-topic ?
It is working now, after removing the QSA, NC and R=301 options.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的重写日志并不能说明什么。从一开始就删除 IP 信息(谁在乎呢?)。并添加重写的开头(=告诉我们将被重写的uri是什么):看看下面日志的第一行。
下面是一个应该更有用的示例:
重写自:
到
您的
.htaccess
文件如下所示:它永远不会工作...尝试这个:
...或者也许采取一切< em>但是斜杠?这可能看起来像这样:
请告诉我这是否有效。
两个提示:
如果您不在托管环境中(= 如果它是您自己的服务器并且您可以修改虚拟主机,而不仅仅是
.htaccess
文件),请尝试使用RewriteLog
指令:它可以帮助您追踪此类问题:我最喜欢的检查正则表达式的工具:
http://www.quanetic.com/Regex (不要忘记选择 ereg(POSIX) 而不是 preg(PCRE)!)
Your rewritelog doesn't tell much. Remove IP information at the beginning (who cares?). And add the beginning of rewrite (= tell us what is the uri that will be rewritten): take a look at the first line of the following log.
Here's an example of what should be more helpful:
Rewrite from:
to
And your
.htaccess
file looks like this:It will never work... try this:
... or maybe how about taking everything but the slash? This may look like that:
Please tell me if this works.
Two hints:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the
.htaccess
files), try to use theRewriteLog
directive: it helps you to track down such problems:My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)