Problem with RewriteCond order and %n in RewriteRule

gboudreau

Well-Known Member
#1
Hi,

Using 2.2.2 (free), with an .htaccess like this:

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} gallery\_remote2\.php
    RewriteCond %{REQUEST_FILENAME} !/main\.php$
    RewriteRule .   -   [L]

    RewriteCond %{THE_REQUEST} \ /d/([0-9]+)-([0-9]+)/([^\/\?]+)(\?.|\ .)
    RewriteCond %{REQUEST_FILENAME} !/main\.php$
    RewriteRule .   /main.php?g2_view=core.DownloadItem&g2_itemId=%1&g2_serialNumber=%2&g2_fileName=%3   [QSA,L]

    RewriteCond %{THE_REQUEST} \ /v/([^?]+)(\?.|\ .)
    RewriteCond %{REQUEST_FILENAME} !/main\.php$
    RewriteRule .   /main.php?g2_view=core.ShowItem&g2_path=%1   [QSA,L]

</IfModule>
If you go to a URL like http://gallery.domain.com:8088/d/5245-14/something.jpg
then the second rule gets trigerred, but the %1, %2 and %3 are empty in the RewriteRule.

Here's what the log shows:

Code:
2006-10-01 15:03:42.660	INFO	[66.130.250.120:61102-0#gallery.domain.com] [REWRITE] Cond: Match 'GET /d/5245-14/something.jpg HTTP/1.1' with pattern '\ /d/([0-9]+)-([0-9]+)/([^\/\?]+)(\?.|\ .)', result: 5
2006-10-01 15:03:42.660	INFO	[66.130.250.120:61102-0#gallery.domain.com] [REWRITE] Cond: Match '/var/www/html/gallery2/d/5245-14/something.jpg' with pattern '/main\.php$', result: -1
2006-10-01 15:03:42.660	INFO	[66.130.250.120:61102-0#gallery.domain.com] [REWRITE] Source URI: 'd/5245-14/something.jpg' => Result URI: '/main.php?g2_view=core.DownloadItem&g2_itemId=&g2_serialNumber=&g2_fileName='
2006-10-01 15:03:42.660	INFO	[66.130.250.120:61102-0#gallery.domain.com] [REWRITE] append query string 'g2_view=core.DownloadItem&g2_itemId=&g2_serialNumber=&g2_fileName='
2006-10-01 15:03:42.660	INFO	[66.130.250.120:61102-0#gallery.domain.com] [REWRITE] Last Rule, stop!
If I simply invert both RewriteCond lines like this:
Code:
    RewriteCond %{REQUEST_FILENAME} !/main\.php$
    RewriteCond %{THE_REQUEST} \ /d/([0-9]+)-([0-9]+)/([^\/\?]+)(\?.|\ .)
Then it works fine:
Code:
2006-10-01 15:01:06.148	INFO	[66.130.250.120:61078-0#gallery.domain.com] [REWRITE] Cond: Match '/var/www/html/gallery2/d/5245-14/something.jpg' with pattern '/main\.php$', result: -1
2006-10-01 15:01:06.148	INFO	[66.130.250.120:61078-0#gallery.domain.com] [REWRITE] Cond: Match 'GET /d/5245-14/something.jpg HTTP/1.1' with pattern '\ /d/([0-9]+)-([0-9]+)/([^\/\?]+)(\?.|\ .)', result: 5
2006-10-01 15:01:06.148	INFO	[66.130.250.120:61078-0#gallery.domain.com] [REWRITE] Source URI: 'd/5245-14/something.jpg' => Result URI: '/main.php?g2_view=core.DownloadItem&g2_itemId=5245&g2_serialNumber=14&g2_fileName=something.jpg'
2006-10-01 15:01:06.149	INFO	[66.130.250.120:61078-0#gallery.domain.com] [REWRITE] append query string 'g2_view=core.DownloadItem&g2_itemId=5245&g2_serialNumber=14&g2_fileName=something.jpg'
2006-10-01 15:01:06.149	INFO	[66.130.250.120:61078-0#gallery.domain.com] [REWRITE] Last Rule, stop!
Any reason for such a behavior ?
Is RewriteRule only supposed to be able to use the %n from the previous line, and no other RewriteCond lines ?

PS This is from a working Gallery 2 installation. The same Rewrite rules work fine on Apache 2, with both RewriteCond lines inverted or not.

Thanks.
 
Last edited:

mistwang

LiteSpeed Staff
#2
Is RewriteRule only supposed to be able to use the %n from the previous line, and no other RewriteCond lines ?
Yes, current rewrite engine implement is like that. We will change our implementation to improve the compatibility with Apache.

Thanks for the bug report and analysis.
 
Top