I have to rewrite some specific URL's on my small website but cannot discover how to get it done despite trying to find quite a while.

The initial url that should be matched up is within this format: http://www.example.com/gallery/?level=picture&id=49

and that i need them to stay in this format: http://www.example.com/index.php?page=gallery&level=picture&id=49

However, the folder also consists of some image files too. I want any URL's pointing straight to images to become left alone, and then any URL's pointing to some php page to become rewritten as above.

I understand what I wish to do, although not how to get it done. Essentially I have to do that inshtaccess:

if(url contains 'gallery/' AND filetype != bmp/jpg/png/etc){
REPLACE '/gallery/' WITH '/index.php?page=gallery&' AND append original query string variables

Any help whatsoever could be greatly appreciated.

This might a little more general than you would like, but it'll also avoid spinning other existing files.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^/gallery/(.+)$    index.php [QSA]

So I've discovered an answer which has taken proper care of this issue for me personally. I've this employed in my .htaccess file now.

RewriteCond %{REQUEST_URI} !\.(bmp|jpg|jpeg|png|gif)$
RewriteRule ^gallery/$ /index.php?page=gallery [R=302,QSA]

The very first line (from what I've been told) excludes the file types listed from struggling with this rule, because when I initially pointed out I didn't want the URL rewritten for images.

The 2nd line requires a url such as this: http://www.yoursite.com/gallery/?level=picture&id=52

and turns it directly into this: http://www.yoursite.com/index.php?page=gallery&level=picture&id=52

also it leaves the initial query string in position, additionally towards the new "page=gallery" variable. Additionally, it does a 302 redirect to ensure that the consumer is proven the right address within their browser.

Unsure if this sounds like useful to anybody, but figured that since i have published asking about this, which i would publish the answer I discovered too.