0

First, I'm trying to add a query param of i=jplx onto any uri that references an image.

And in the first two tests below it is working as expected.

However, when I try to add commands that would both add the i=jplx param plus convert the uri to pass as a param (index.php?q=system/files/&i=jplx) the next two tests (3 and 4) don't return as expected.

I've been using this for testing: https://htaccess.madewithlove.be

Any ideas on what I have wrong in the htaccess commands?

Test 1

https://some.com/sites/default/files/Slide074.png

RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$
RewriteRule ^(.*)$ $1?i=jplx [QSA]

Result 1 - as expected

https://some.com/sites/default/files/Slide074.png?i=jplx

...

Test 2

https://some.com/sites/default/files/Slide074.png?abc

RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$
RewriteRule ^(.*)$ $1?i=jplx [QSA]

Result 2 - as expected

https://some.com/sites/default/files/Slide074.png?i=jplx&abc

...

Test 3

https://some.com/sites/default/files/Slide074.png

RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$
RewriteRule ^(.*)$ $1?i=jplx [QSA]

RewriteCond %{REQUEST_URI} !^/sites/default/files/js/
RewriteCond %{REQUEST_URI} !^/sites/default/files/css/
RewriteRule ^sites/default/files/(.*)$ index.php?q=system/files/$1 [L,QSA]

Result 3

https://some.com/index.php?q=system/files/Slide074.png

Expected 3

https://some.com/index.php?q=system/files/Slide074.png&i=jplx

...

Test 4

https://some.com/sites/default/files/Slide074.png?abc

RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$
RewriteRule ^(.*)$ $1?i=jplx [QSA]

RewriteCond %{REQUEST_URI} !^/sites/default/files/js/
RewriteCond %{REQUEST_URI} !^/sites/default/files/css/
RewriteRule ^sites/default/files/(.*)$ index.php?q=system/files/$1 [L,QSA]

Result 4

https://some.com/index.php?q=system/files/Slide074.png&abc

Expected 4

https://some.com/index.php?q=system/files/Slide074.png&i=jplx&abc
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Greg Mercer
  • 914
  • 1
  • 8
  • 11

1 Answers1

0

The L flag tends restarts a rewrite-operation loop. Instead use the END flag:

RewriteCond %{REQUEST_URI} !^/sites/default/files/js/
RewriteCond %{REQUEST_URI} !^/sites/default/files/css/
RewriteRule ^sites/default/files/(.*)$ index.php?q=system/files/$1 [END,QSA]
                                                                    ###

(from test 4)

If you enable error logging with level debug rewrite:trace8 and grep for rewrite apache shows all steps it is going through, here the excerpt (+ reformat for readability) showing the effect of the END flag:

[rewrite:trace2] [pid ...] mod_rewrite.c(483): [client 127.0.0.1:49538] 127.0.0.1 - - [127.0.0.1/sid#555c63b3f410]
    [rid#555c63bbb6e0/initial] [perdir /usr/local/apache2/htdocs/] 
        rewrite 'sites/default/files/Slide074.png/default/files/Slide074.png'
            -> 'index.php?q=system/files/Slide074.png/default/files/Slide074.png'
[rewrite:trace3] [pid ...] mod_rewrite.c(483): [client 127.0.0.1:49538] 127.0.0.1 - - [127.0.0.1/sid#555c63b3f410]
    [rid#555c63bbb6e0/initial] 
        split uri=index.php?q=system/files/Slide074.png/default/files/Slide074.png 
            -> uri=index.php, args=q=system/files/Slide074.png/default/files/Slide074.png&i=jplx&ab
[rewrite:trace3] [pid ...] mod_rewrite.c(483): [client 127.0.0.1:49538] 127.0.0.1 - - [127.0.0.1/sid#555c63b3f410]
    [rid#555c63bbb6e0/initial] [perdir /usr/local/apache2/htdocs/]
        add per-dir prefix: index.php -> /usr/local/apache2/htdocs/index.php
[rewrite:trace8] [pid ...] mod_rewrite.c(483): [client 127.0.0.1:49538] 127.0.0.1 - - [127.0.0.1/sid#555c63b3f410]
    [rid#555c63bbb6e0/initial] [perdir /usr/local/apache2/htdocs/] 
        Rule has END flag, no further rewriting for this request

Trivia: Your question reminded me of an earlier answer shared on the site by myself Mod_Rewrite unexpected behavior L flag.

hakre
  • 193,403
  • 52
  • 435
  • 836