Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond to Match/Extract a Query String

mod rewrite RewriteCond RewriteRule

         

ragomez

1:09 pm on Nov 3, 2007 (gmt 0)

10+ Year Member



I'm trying to rewrite a URL with a query string into a URL without a query string - the opposite of what I normally use mod rewrite for...

I've read that, "You cannot use a RewriteRule to match a query string from a dynamic url" and therefore must use RewriteCond. I've never used RewriteCond and I can't seem to get it.

Here is my particular problem:

OLD (REQUESTED) URL:
http://example.com/photos/watermark.php?file=500/431-file-6-11_01.jpg

NEED TO REWRITE TO (WORKING URL):
http://example.com/photos/dir/500/431-file-6-11_01.jpg

Here is what I've muddled so far:

RewriteCond %{QUERY_STRING} ^file\=([\d]+)/(.+)$
RewriteRule ^$ /photos/dir/%1/%2 [L]

Need I say it's not working? Any ideas? Where do I go from here?

Thanks in advance for your help!

[edited by: encyclo at 5:47 pm (utc) on Nov. 6, 2007]
[edit reason] switched to example.com [/edit]

jdMorgan

1:36 pm on Nov 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming the code is in example.com/.htaccess, the following should do what you describe. However, this assumes that there is a static file for each image.

RewriteCond %{QUERY_STRING} ^file=([0-9]+)/([^\.]+\.jpg)$
RewriteRule ^photos/watermark\.php$ /photos/dir/%1/%2 [L]

It's more likely that you want an external redirect (to 'correct' the URL in search engine listings) and that these new static URLs need to be subsequently rewritten (internally) to a script. If that's the case, then the above, while it is the answer to your question, is not the solution to your real problem.

Jim

ragomez

2:08 pm on Nov 5, 2007 (gmt 0)

10+ Year Member



Thanks for the reply!

The code you submitted did not work. After a couple more hours and help from another forum, I finally got it to work using the following:

RewriteCond %{QUERY_STRING} ^(.*&)?file=([^&]+)(&.*)?$ [NC]
RewriteRule ^photos/watermark\.php$ /photos/data/%2? [NC,L]

And no, the resulting static URL now points to actual existing directory/file structure. The "watermark" script was changed and caused thousands of legacy image links to stop working. Fortunately, the legacy links contained all the dir and file info needed to redirect to actual image locations... Except without a "Watermark"...

[edited by: jdMorgan at 2:13 pm (utc) on Nov. 5, 2007]
[edit reason] Disabled smilies in code [/edit]

jdMorgan

2:35 pm on Nov 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



based on your working code, it looks like your requirements description above was not quite right. The regex patterns must be exactly correct, and so the requirements must be exactly specified as well.

You can shorten that code and make if more efficient:


RewriteCond %{QUERY_STRING} &?file=([^&]+) [NC]
RewriteRule ^photos/watermark\.php$ /photos/data/%1? [NC,L]

Also, be careful using the [NC] flags here; By making your pattern-matching case-insensitive, you are creating duplicate content -- More than one URL resolving to the same content.

Jim