Match against path only

Topics: User Forum
Aug 4, 2011 at 2:18 PM

Is it at all possible to match against just the path? I am finding myself getting 404s when a querystring is included in the URL and I didn't specially allow for query strings in the pattern. Even more troublesomely, the same patters to accommodate for querystrings doesn't seem to work for each rule for some reason.

So ideally, i'd like to just match against the path, and pass-through the query string if QSA is on. Otherwise.. what's the best pattern to allow for arbitrary querystrings?

For reference here are the contents of my iirf.ini in question

RewriteRule ^/([0-9a-zA-Z_-]*)/about-us$ /about-us.aspx?slug=$1 [QSA,L]
RewriteRule ^/([0-9a-zA-Z_-]*)/contact$ /contact.aspx?slug=$1 [QSA,L]

RewriteRule ^/blog/((\?.*)|())$ /blog.aspx [QSA,L]
RewriteRule ^/blog/([0-9]+/[0-9]+/[0-9a-zA-Z_-]+)$ /blog.aspx?slug=$1 [L]
RewriteRule ^/blog/([0-9]+/[0-9]+/[0-9a-zA-Z_-]+)(\?.+)$ /blog.aspx?slug=$1 [QSA,L]
RewriteRule ^/consumer-info/((\?.*)|())$ /consumer-info.aspx [QSA,L]
RewriteRule ^/consumer-info/([0-9a-zA-Z_-]+)/$ /consumer-info.aspx?slug=$1 [QSA,L]
RewriteRule ^/consumer-info/([0-9a-zA-Z_-]+/[0-9a-zA-Z_-]+)((\?.*)|())$ /consumer-info.aspx?slug=$1 [QSA,L]
RewriteRule ^/products/([0-9a-zA-Z_-]+)/$ /products/default.aspx?brand=$1 [QSA,L]
RewriteRule ^/products/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/$ /products/default.aspx?brand=$1&type=$2&category=$3 [L]
RewriteRule ^/products/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)$ /products/default.aspx?brand=$1&type=$2&category=$3&product=$4 [L]

RewriteRule ^/rss/([a-z]+).rss$ /rss.aspx?mode=$1
RewriteRule ^/sitemap[.](html|txt|xml)$ /sitemap.aspx?type=$1

RewriteCond %{REQUEST_FILENAME}.aspx -f
RewriteRule ^/([0-9a-zA-Z_-]*)((\?.*)|())$ /$1.aspx [QSA,L]

Coordinator
Aug 7, 2011 at 11:40 PM

In any regex pattern where you have a $ , IIRF requires a successful match to end right there.  What it means is ^/([0-9a-zA-Z_-]*)/about-us$ will never match a URL with a query string, because about-us must be the final string according to that pattern.  The same is true for any other of your patterns that end in $. 

If you would like to optionally allow a querystring, and you don't care what it is you can use a regex pattern like this:

^/([0-9a-zA-Z_-]*)/about-us($|\?)

What that says is: the string about-us must be followed by end-of-line (implied by $), or by a question-mark.  The vertical pipe ( | ) symbol denotes an "alternation" in regex-speak, which is like a logical OR.   It says either A or B.    In this particular case, A is end-of-line, and B is a question-mark.   In the regex, the ? must be preceded with a backslash to "escape" it.  So the result is ($|\?) matches either end-of-line or question-mark.

You have something similar in this pattern:

^/blog/((\?.*)|())$

This is nearly equivalent:  it uses an alternation.  The first option is a question-mark followed by a series of zero of more characters of any kind.  Here again the ? is backslash-escaped.  Option B in the alternation is (), which matches "nothing".  And then following either of those options, is the $, denoting end-of-line.  This would be nearly equivalent to:

^/blog/($|\?)

...except that the latter does not capture the query string.  In your case you don't use the captured query string explicitly (QSA appends it implicitly, you might say), so the two patterns would be functionally equivalent for your purposes.

 

Aug 15, 2011 at 1:06 AM
Thank you for your help.

On Sunday, August 07, 2011 06:40:44 pm you wrote:
> From: Cheeso
>
> In any regex pattern where you have a $ , IIRF requires a successful match to end right there. What it means is ^/([0-9a-zA-Z_-]*)/about-us$ will never match a URL with a query string, because about-us must be the final string according to that pattern. The same is true for any other of your patterns that end in $. If you would like to optionally allow a querystring, and you don't care what it is you can use a regex pattern like this:^/([0-9a-zA-Z_-]*)/about-us($|\?)What that says is: the string about-us must be followed by end-of-line (implied by $), or by a question-mark. The vertical pipe ( | ) symbol denotes an "alternation" in regex-speak, which is like a logical OR. It says either A or B. In this particular case, A is end-of-line, and B is a question-mark. In the regex, the ? must be preceded with a backslash to "escape" it. So the result is ($|\?) matches either end-of-line or question-mark.You have something similar in this pattern:^/blog/((\?.*)|())$This is nearly equivalent: it uses an alternation. The first option is a question-mark followed by a series of zero of more characters of any kind. Here again the ? is backslash-escaped. Option B in the alternation is (), which matches "nothing". And then following either of those options, is the $, denoting end-of-line. This would be nearly equivalent to:^/blog/($|\?)...except that the latter does not capture the query string. In your case you don't use the captured query string explicitly (QSA appends it implicitly, you might say), so the two patterns would be functionally equivalent for your purposes.
>
>