Trying to exclude query string

Topics: Developer Forum
Nov 9, 2010 at 5:51 PM

I'm having trouble excluding any query string in my urls.

Using the following:






([^\?\/]+) should match $1 (test)
((/[^\?](.*)$)|(/$))$ should match and ignore ?money=null

expected output:

/index.html?param1=$1 (ignore the ? and anything after that)

But the rule is just ignored. Any ideas how to fix?

FYI, Here's the list of rules I'm trying to use


# handle case with no query string .  This rule fires the first time through.
RewriteRule ^/(\?|$)                /index.html      [I,L,U]

# handle case with 1 query string
RewriteRule ^/([^\?\/]+)((/[^\?](.*)$)|(/$))$        /index.html?param1=$1        [I,L,U]
# handle case with 2 query strings
RewriteRule ^/([^\/]+)/([^\?\/]+)((/[^\?](.*)$)|(/$))$    /index.html?param1=$1&param2=$2    [I,L,U]

# handle case with 3 query strings
RewriteRule ^/([^\/]+)/([^\/]+)/([^\?\/]+)((/[^\?](.*)$)|(/$))$ /index.html?param1=$1&param2=$2&param3=$3    [I,L,U]

Thanks for your help.

Nov 9, 2010 at 6:01 PM
Edited Nov 9, 2010 at 6:07 PM

grab a regex tester and play around with it.

This regex:


...seems not quite right.  It says (I think),

fragment meaning
^/ beginning-of-line followed by slash
([^\?\/]+) a sequence of one or more chars that is neither ? nor /
((/[^\?](.*)$|(/$))$ either:
  • a slash, followed by anything that is not a ?, followed by zero or more of anything, followed by end-of-line, OR
  • a slash, followed by end-of-line


So, no, I don't think it should match /test/?money=null, because /?money=null doesn't match the last thing in that table.

Regex can be tricky. Visualizing the regex matches with an interactive tool is really helpful. I recommend a couple in the IIRF documentation. Try them out.

Nov 9, 2010 at 6:16 PM


I think I got it thanks to your explanation.



should be this:



I had a feeling it was a regex issue, seeing as how I suck at it. :)

Nov 9, 2010 at 6:24 PM

Here's a good basic (and free) regex tool:

It looks like this when I type in your regex and sample URL:

(No match).   

If I change the regex to:  ^/([^/\?]+)(/\?.*)$

...then it matches. 

 But I think you want to handle the case where the query string (and question mark) is present as well as when it is not present. 

So you'll need a choice, and you'll need to handle all the cases the way you want.

good luck.



Nov 9, 2010 at 6:30 PM

our messages crossed!  Glad you got it sorted.