3

我在 mod_rewrite 正则表达式中使用匹配量词 {},如果量词匹配 1 或 2 次,则重写规则工作,如果匹配 3 次或更多次则不工作。为什么?

.htaccess 文件示例:

这项工作(我需要 mydomain.com/download.ex

RewriteEngine On
RewriteRule ^download\..{1,2}$ /download.php [L]

但这不起作用,500 错误,只更改了 2 到 3 个最大量词(我需要 mydomain.com/download.exe

RewriteEngine On
RewriteRule ^download\..{1,3}$ /download.php [L]

这太棒了,但它是真实的。为什么会这样?

ps版本:

root@andrey:/var/www/default/www# apache2 -v
Server version: Apache/2.4.7 (Ubuntu)
Server built:   Jul 22 2014 14:36:38

root@andrey:/var/www/default/www# uname -a
Linux mydomain.com 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 x86_64          x86_64 GNU/Linux

root@andrey:/var/www/default/www# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.1 LTS
Release:        14.04
Codename:       trusty  
4

1 回答 1

0

正则表达式\.{1,3}工作正常,但无法正常工作,因为它也匹配/download.php并导致 mod_rewrite 无限循环,从而导致 500 错误。

为了克服这个问题,当扩展名是时,有一个负面的前瞻性来停止重写.php

RewriteEngine On
RewriteBase /

RewriteRule ^download\.(?!php$).{1,3}$ download.php [L,NC]
于 2014-11-13T06:03:24.583 回答