Multiple Replacements in one RewriteRule?

May 8, 2007 at 8:29 PM
I want to do something very simple. I want to replace all instances of ? with &. Normally, I might write a RegEx like the following:
\?+
and I would be able to supply & as a replacement char. However, after looking at the documentation and testing it... it doesn't work for me.

I found the following piece of documentation and it leads me to believe that what I want to do isn't possible.

The replacement string can contain:

- constant text

- references to the matched substrings in the input, in the form
of $n, where n is a digit. (Eg $1, $2, $3...). These are
called back-references.

- references to the matched substrings in the most recently
evaluated RewriteCond associated to that RewriteRule. These
take the form %n, where n is a digit. (Eg %1, %2, %3...)

- references to server variables. Eg, %{QUERY_STRING} in the
replacement pattern embeds the query string of the incoming
request, into the output (rewritten) URL.

Coordinator
May 11, 2007 at 7:53 PM
Use v1.2.12 and escape the ampersand (%26).
Oct 21, 2008 at 8:32 PM
Edited Oct 21, 2008 at 8:34 PM
I need to also replace all instances of ? with &. I will download the latest version of IIRF. However, I am unsure of how to go about doing this.

For example I have the following rule:
RewriteRule ^/(Activities-for-Life|Athletic|Sensory-Solutions|Special-Populations)(/[^(&|\?)]+|)(.*)    /search.asp?category=$1$2$3 [L,I]

test URL: /Special-Populations/Accessible-Desks?PageNo=2

testDriver outputs: /search.asp?category=Special-Populations/Accessible-Desks-&PageNo=2

How would I change the rule so that the second ? is changed to &?
Coordinator
Oct 21, 2008 at 11:49 PM

I think I suggested to you on another thread to use 2 rules.  How about this in your ini file:

# This rule takes care of the case with a category and sub-category, when there is no query string.
# Here, a matching URL ends in a slash or a ? or neither or a sequence of '/?' .
# With this rule, 
#      '/Special-Populations' ==> '/search.asp?category=Special-Populations'
# And
#      '/Special-Populations/Accessible-Desks' ==> '/search.asp?category=Special-Populations/Accessible-Desks'
#
RewriteRule ^/(Activities-for-Life|Athletic|Sensory-Solutions|Special-Populations)(/[^&|\?]+?)?(/{0,1})(\?{0,1})$    /search.asp?category=$1$2 [L,I]



# This rule takes care of the case with a category and sub-category, with a query string.
# Here, the question mark in the URL can be preceded by a slash or not.  
# With this rule, 
#      '/Special-Populations?PageNo=2' ==> '/search.asp?category=Special-Populations&PageNo=2'
# And
#      '/Special-Populations/Accessible-Desks?PageNo=2' ==> '/search.asp?category=Special-Populations/Accessible-Desks&PageNo=2'
#
RewriteRule ^/(Activities-for-Life|Athletic|Sensory-Solutions|Special-Populations)(/[^&|\?]+?)?(/{0,1})\?([^\?]+)$    /search.asp?category=$1$2&$4 [L,I]

I tested it with these sample URLs:

# Incoming URL                                       Expected Result
#---------------------------------------------------------------------- 
#
/Special-Populations                                 /search.asp?category=Special-Populations
/Special-Populations?                                /search.asp?category=Special-Populations
/Special-Populations/                                /search.asp?category=Special-Populations
/Special-Populations/?                               /search.asp?category=Special-Populations
/Special-Populations/Accessible-Desks                /search.asp?category=Special-Populations/Accessible-Desks
/Special-Populations/Accessible-Desks?               /search.asp?category=Special-Populations/Accessible-Desks
/Special-Populations/Accessible-Desks/               /search.asp?category=Special-Populations/Accessible-Desks
/Special-Populations/Accessible-Desks/?              /search.asp?category=Special-Populations/Accessible-Desks
/Special-Populations?PageNo=2                        /search.asp?category=Special-Populations&PageNo=2
/Special-Populations/Accessible-Desks?PageNo=2       /search.asp?category=Special-Populations/Accessible-Desks&PageNo=2
/Special-Populations/Accessible-Desks?PageNo=2&class=OnClearance       /search.asp?category=Special-Populations/Accessible-Desks&PageNo=2&class=OnClearance

On the other hand, I dont' know why you would want PageNo=2 as a query string in the URLs you expose. why not just /Special-Populations/Accessible-Desks/2

or, /Special-Populations/Accessible-Desks/Clearance

But I will leave that to you.

Oct 22, 2008 at 2:25 PM
OK, Thanks alot.

I see you improved the rules. I guess I wanted to know if there is some magical way to search and replace a specified character (like replace in ASP, or str_replace in PHP) with the URL rewrite. I think that might enable you to combine these 2 rules into 1.

I know it makes more sense (and looks better) to just use a slash and the page number at the end. I guess I would just tweak the rule to look for the digit(s) at the end right? I thought of that when I was working on this yesterday. But common sense does not kick in when your tired of working all day. SO I think I will do just that.

However categories can have up to 4 levels, so that means 4 slashes, and there is nothing stopping them from making a sub-category that starts with a number.

example:
     /Athletic/Athletic-Aids/2-Timers-%2F-Scoreboards/2

Which is why I figured is would be easier for me (being a novice REGEXP writer) to just leave it as is was, as I am not sure how I would modify the rule below, to properly parse the above.
RewriteRule ^/(Activities-for-Life|Athletic|Sensory-Solutions|Special-Populations)(/[^&|\?]+?)?(/{0,1})\?([^\?]+)$    /search.asp?category=$1$2&$4 [L,I]

Coordinator
Oct 22, 2008 at 2:38 PM
With that testdriver.exe tool, you should be able to try a bunch of combinations of RegEx to see what works for the URLs you want.