【问题标题】:mod_rewrite not appending queriesmod_rewrite 不附加查询
【发布时间】: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


    【解决方案1】:

    您的重写日志并没有说明太多。删除开头的 IP 信息(谁在乎?)。并添加 rewrite 的开头(= 告诉我们将要重写的 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
    

    我最喜欢的检查正则表达式的工具:

    http://www.quanetic.com/Regex(别忘了选择 ereg(POSIX) 而不是 preg(PCRE)!)

    【讨论】:

    • 这不起作用。我已经打开了我的重写日志,你可以在最初的帖子中看到。
    • 请查看我所做的编辑,这可能有助于理解问题。
    • 更新了我的答案,我的答案中有一个问题;)
    • 感谢您的帮助,我现在已经解决了这个问题。 (见上文)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 2012-02-05
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    相关资源
    最近更新 更多