【问题标题】:mod rewrite not reading the rule from htaccess filemod rewrite 不从 htaccess 文件中读取规则
【发布时间】:2013-08-23 13:57:07
【问题描述】:

我正在使用 WAMP(apache 2.2.9 和 PHP 5.4.17)。我有几个在生产服务器 (CentOS) 上运行的简单 mod 重写规则,但在我的 Windows 机器上,一条规则被忽略了。我花了 3 天的时间寻找解决方案,但没有奏效。

这是模组重写规则

Options -Indexes FollowSymLinks MultiViews

RewriteEngine on
RewriteBase /mysite/

RewriteRule ^reviews/([a-zA-Z0-9_-]+)/([0-9]+)/(.*).html$ page-review.php?t=$1&m=$2&s=$3 [L] #This is being ignored by Apache
RewriteRule ^reviews/?$ reviews.php [L]
RewriteRule ^reviews/([a-zA-Z0-9_-]+)-reviews/?$ reviews.php?r=$1 [L]
RewriteRule ^file/([a-zA-Z0-9_-]+)/s([0-9]+)-([a-zA-Z0-9_-]+)/f([0-9]+)-(.*).html$ file.php?c=$1&s=$2&st=$3&&m=$4&t=$5 [L]

Apache 忽略了第一个重写规则。令人惊讶的是,当我从 htaccess 中删除所有内容时,它仍然会读取我的规则。我已经打开了 mod rewrite log,这里是日志详细信息

log for http://localhost/mysite/reviews/product/11/samsung.html

127.0.0.1 - - [23/Aug/2013:14:37:34 +0100] [localhost/sid#453140][rid#1821000/subreq] (1) [perdir /mydir/] pass through /mydir/reviews.php
127.0.0.1 - - [23/Aug/2013:14:37:34 +0100] [localhost/sid#453140][rid#17cc4f0/initial] (1) [perdir /mydir/] pass through /mydir/reviews.php
127.0.0.1 - - [23/Aug/2013:14:37:34 +0100] [localhost/sid#453140][rid#17d0500/initial] (1) [perdir /mydir/] pass through /mydir/reviews.php
127.0.0.1 - - [23/Aug/2013:14:37:34 +0100] [localhost/sid#453140][rid#1835050/initial] (1) [perdir /mydir/] pass through /mydir/reviews.php

正如您在上面看到的,它不是将上述请求重写为 page-review.php,而是读取 reviews.php 文件

这是另一个可以正常工作的请求

log for http://localhost/mysite/file/downloads/software/f5983-docs.html

127.0.0.1 - - [23/Aug/2013:14:39:42 +0100] [localhost/sid#453140][rid#47f4188/initial] (2) [perdir /mydir/] rewrite 'file/downloads/software/f5983-docs.html' -> 'file.php?c=downloads&s=45&st=software&&m=5983&t=docs'
127.0.0.1 - - [23/Aug/2013:14:39:42 +0100] [localhost/sid#453140][rid#47f4188/initial] (2) [perdir /mydir/] trying to replace prefix /mydir/ with /mysite/
127.0.0.1 - - [23/Aug/2013:14:39:42 +0100] [localhost/sid#453140][rid#47f4188/initial] (1) [perdir /mydir/] internal redirect with /mysite/file.php [INTERNAL REDIRECT]
127.0.0.1 - - [23/Aug/2013:14:39:42 +0100] [localhost/sid#453140][rid#47ed3e0/initial/redir#1] (1) [perdir /mydir/] pass through /mydir/file.php

【问题讨论】:

    标签: .htaccess apache2


    【解决方案1】:

    规则看起来还不错。试试这个稍加修改的代码:

    Options -Indexes +FollowSymLinks -MultiViews
    
    RewriteEngine on
    RewriteBase /mysite/
    
    RewriteRule ^reviews/(\w+)/([0-9]+)/([^.]+)\.html$ page-review.php?t=$1&m=$2&s=$3 [L,QSA,NC]
    RewriteRule ^reviews/?$ reviews.php [L,NC]
    RewriteRule ^reviews/(\w+)-reviews/?$ reviews.php?r=$1 [L,QSA,NC]
    RewriteRule ^file/(\w+)/s([0-9]+)-(\w+)/f([0-9]+)-([^.]+)\.html$ file.php?c=$1&s=$2&st=$3&&m=$4&t=$5 [L,QSA,NC]
    

    顺便说一句,您最后一条规则中的正则表达式与 file/downloads/software/f5983-docs.html 不匹配

    【讨论】:

    • 谢谢!我刚刚用 -MultiViews 替换了 MultiViews,它解决了我的问题。想知道 -MultiViews 的范围是什么?我注意到您已将 (.*) 替换为 ([^.]+) 您能解释一下这条规则以及为什么 [NC]?
    • NC 用于忽略大小写比较。在使用否定的正则表达式中,[^.]+ 通常比.* 更安全(它有时可以抓住一切)。此外,MultiViews 应该在使用 mod_rewrite 时关闭,因为当 MultiViews 开启时,Apache 会进行自己的 URL 重写。
    猜你喜欢
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多