query string after rewrite

Topics: Developer Forum, User Forum
Jul 16, 2009 at 2:22 PM

Hi All,

I'm trying to add a query string at the end of my initial rewrite for items such as campaign tracking on google.

My Working Rule for normal items:

RewriteRule ^/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ /index.cfm?page=productdetail2&categoryslug=$1&productslug=$2

This rewrites to www.domain.com/Category/Product.html

I now require www.domain.com/Category/Product.html?utm_source=xxxx&utm_medium=xxx&utm_campaign=xxx

I've got this going to a point:

RewriteRule ^/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html\?(.+)$  /index.cfm?page=productdetail2&categoryslug=$1&productslug=$2?$3 [L]

It's not grabbing my productslug and i'm unsure about the extra variables.

Any advice is appreciated.




Jul 16, 2009 at 3:03 PM

Answered my own question, the trailing slash was causing the issue.

Here's the working version:

RewriteRule ^/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html?(.+)$  /index.cfm?page=productdetail2&categoryslug=$1&productslug=$2&$3

Let me know if i'm wrong.

Jul 16, 2009 at 6:45 PM
Edited Jul 16, 2009 at 6:47 PM

If it works for you, then you're not wrong.

But I think you may want to look at it again. First, the dot preceding the html is a wildcard, and it matches anything. I think you want to escape it to match only "dot". Also, the html? sequence does not match "html?" but instead matches "htm" or "html". The question mark in the pattern is a quantifer, implying "zero or one". I don't think that's what you want.

You make up for it by capturing whatever question mark there might be, in the (.*) that follows. But this ends up NOT being exactly what you want to append to the rewritten URL, because it will capture the question mark. So that the result is something like


ya see? there's an extra ? in the result.

You can do what you want by making the ? optional, and then not including it in the result, like this:

  RewriteRule ^/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\.html\??(.*)$ 

But in the case of an empty query string in the original, you will get a (rewritten) result that ends in a trailing &. Not a disaster, but not precisely what you want. You'd need another rule to capture both styles.

There's a [QSA] modifier in the latest version that lets you do this with much less trouble.

It says "append whatever query string that was in the original URL to the rewrite result". So...

  RewriteRule ^/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\.html 
            /index.cfm?page=productdetail2&category=$1&product=$2  [QSA]

Notice the pattern does not end in $. That means, there may be something in the original URL that follows .html. That something, if it starts with a ?, is picked up by IIRF and appended to the result of the rewrite. You'll never get a trailing & and you won't get an embedded ? either.

Try it, it's in the latest v1.2.16

Jul 16, 2009 at 11:00 PM

Thanks Cheeso,

I didn't know enough about QSA when i read through the example but it seems like the perfect answer.

I'll upgrade my IIRF version and try it out.

The 2nd option you gave me works nicely too.



Jul 18, 2009 at 12:07 AM

I am basically trying to do the same thing (http://iirf.codeplex.com/Thread/View.aspx?ThreadId=62783)

however, after I save the .ini file and restart IIS, I am getting a page not found

Jul 18, 2009 at 3:42 AM

I replied on the other thread, to you, kpirnie.

Oct 2, 2009 at 12:49 PM

Cheeso U Rocks!