Apache mod_rewrite regex limit match max quantifier?

708 views Asked by At

I use match quantifier {} in mod_rewrite regex, rewrite rule work if quantifier match up 1 or 2 times, and not work if match up 3 and more times. Why?

Example .htaccess file:

This work (i require mydomain.com/download.ex):

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

But this not work, 500 error, only changed 2 to 3 max quantifier (i require mydomain.com/download.exe):

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

It's fantastic, but it this real. Why it is so?

p.s. Versions:

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  
1

There are 1 answers

3
anubhava On BEST ANSWER

Regex \.{1,3} is working fine but it is not working because it also matches /download.php and causes mod_rewrite to loop infinitely thus resulting in 500 error.

To overcome this have a negative lookahead to stop rewriting when extension is .php:

RewriteEngine On
RewriteBase /

RewriteRule ^download\.(?!php$).{1,3}$ download.php [L,NC]