3

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  
Andrey
  • 1,183
  • 1
  • 9
  • 10

1 Answers1

0

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • If this is because of loop infinitely, why is first variant ({1,2}) working fine? – Andrey Nov 13 '14 at 06:53
  • Because `\.{1,2}` will not match `.php` only if you give range >= 3 it will match `.php`. Have you tested the solution? – anubhava Nov 13 '14 at 07:10
  • 1
    Thanks a lot, I understood the problem, then it will be easier to just rename download.php _download.php `RewriteRule ^download\..{1,3}$ /_download.php [L]` – Andrey Nov 13 '14 at 07:23