Mod rewrite mod_rewrite不附加查询
我有一个网站,我正试图将search/ks3/7/example主题重写为search.php?ks=ks3&year=7&topic=example主题Mod rewrite mod_rewrite不附加查询,mod-rewrite,apache2,Mod Rewrite,Apache2,我有一个网站,我正试图将search/ks3/7/example主题重写为search.php?ks=ks3&year=7&topic=example主题 (1) [perdir /data/shared/chemistry/] pass through /data/shared/chemistry/search.php (3) [perdir /data/shared/chemistry/] add path info postfix: /data/shared/chemistry/searc
(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 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]
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$\u GET['ks']、$\u GET['topic']和$\u GET['year']不包含任何数据
我的apache错误日志显示PHP“未定义索引ks”中的错误等
我的重写日志(设置为详细度9)显示:
我的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>
这是有效的
我还在服务器上运行了4台其他虚拟主机,其中3台只是网站,1台是transmission的web界面
我正在运行Ubuntu服务器11.11
编辑:
我认为问题可能与运行反向代理和此相关。例如,如果反向代理将192.168.0.10:81(我的服务器地址和端口81 vhost)更改为transmission/web,那么事实上transmission运行在192.168.0.10/transmission/web,这既是vhost,也是运行在192.168.0.10/(本文中讨论的)的站点的子目录,可能导致重写模块中发生冲突。如果你认为这可能是问题所在,你有什么解决办法吗
编辑:
这是我单击search/ks3/7/example主题链接时发生的所有事情的完整日志
(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主题重写为search.php/ks3/7/example-topic,因此search/ks3/7/example主题不满足
^search/([^/]+)/([^/]+)/([^/]+)
规则。我已将规则更改为
^search.php/([^/]+)/([^/]+)/([^/]+)
这是有效的
您知道如何停止apache将search/ks3/7/example-topic重写为search.php/ks3/7/example-topic吗
在删除QSA、NC和R=301选项后,它现在可以工作了。您的重写日志说明不了什么。从一开始就删除IP信息(谁在乎?)。并添加rewrite的开头(=告诉我们要重写的uri是什么):查看下面日志的第一行 下面是一个更有用的示例:
改写自:
search/ks3/7/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 [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的工具:
(别忘了选择ereg(POSIX)而不是preg(PCRE)!这行不通。我已打开我的重写日志,您可以在最初的帖子中看到。请查看我所做的编辑,这可能有助于理解问题。更新了我的答案,在我的答案中添加了一个问题;)谢谢你的帮助,我现在已经解决了这个问题。(见上文)
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On