Forum Moderators: phranque

Message Too Old, No Replies

Trouble rewriting

Help rewriting url

         

gscapin

5:48 pm on Jun 17, 2011 (gmt 0)

10+ Year Member



RegEx and mod_rewrite Gurus:

What is the RewriteCond and RewriteRule for the following situation?

I want to swap out +M33+ with +M44+, so for the this URL :

testdomain.com/CGI-BIN/LANSAWEB?PROCFUN+WWOP+WEBFUNC+M33+FUNCPARMS+WEBTOT(S0030):3+WESIP(S0030):2

I want to return the following :

testdomain.com/CGI-BIN/LANSAWEB?PROCFUN+WWOP+WEBFUNC+M44+FUNCPARMS+WEBTOT(S0030):3+WESIP(S0030):2

Thanks for your time.

lucy24

6:49 pm on Jun 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ugh, what a lot of plusses. Not because it looks ugly but because every single one of them would need to be escaped if you were searching for the exact text ;)

In English:

If any address contains the sequence "+M33+" replace it with "+M44+" without changing anything else? And that's the only variable? Do this for all addresses, and for any location within the address? Would the string ever occur more than once, and if so are you replacing all of them?

In the simplest form, with just one +M33+ per address, you wouldn't even need to set conditions. Simply

RewriteRule \+M33\+ +M44+ [L]

Escape the plusses in the first part, don't escape them in the second. (Some varieties of RegEx don't care if you escape things that don't need escaping, but Apache can get huffy.) If there is a possibility of other stuff needing to be rewritten, leave off the [L].

gscapin

7:19 pm on Jun 17, 2011 (gmt 0)

10+ Year Member



I agree, lots of plusses. Unfortunately, I'm stuck with it :(

This solution looks good, I will give it a shot. Don't know why I thought I needed a condition...

Thanks.

gscapin

8:08 pm on Jun 17, 2011 (gmt 0)

10+ Year Member



That didn't work, I'll have to figure out how to adjust the rule without impacting web traffic.

g1smd

8:24 pm on Jun 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to capture the URL parts you want to re-use by using RegEx patterns, then use $1 and $2 to make the substitution.

You can't guess the syntax. However there are 20 000 previous redirect examples in this forum to guide you.

The + symbol has special meaning in a RegEx pattern. Literal + must be escaped as \+ here.