Question

stupid question

how can put:

RewriteRule ^$ index.php?url=$1&%{QUERY_STRING} [QSA]

into :

RewriteRule ^$ index.php?url=*.(jpe?g|png|bmp)&%{QUERY_STRING} [QSA]

rewrite url http://site.com/folder/index.php?url=123.jpg&do=xxx

to

http://site.com/folder/123.jpg?do=xxx

first rule can do this,but I need to prevent gif file

Was it helpful?

Solution

So you are looking to allow the browser to supply an image URL like folder/123.jpg (or .gif, .png, .bmp) to index.php?url=123.jpg and append the existing query string:

RewriteEngine On
RewriteRule ^folder/([^.]+)\.(jpg|jpeg|png|bmp)$ folder/index.php?url=$1.$2 [L,QSA]

The pattern ([^.]+) captures everything up to the . into $1, and the extension is captured into $2. [QSA] will append the existing query string do=xxx without you having to do anything to append it manually.

This can be simplified in Apache2 with a non-capturing group (?:) so the whole thing is caught in $1.

RewriteRule ^folder/([^.]+\.(?:jpg|jpeg|png|bmp))$ folder/index.php?url=$1 [L,QSA]

Or you could apply it conditionally:

# If the request ends in .jpg, .bmp, .png...
RewriteCond %{REQUEST_URI} \.(jpe?g|bmp|png)$
RewriteRule ^folder/(.*)$ folder/index.php?url=$1 [L,QSA]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top