Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

htaccess 301 redirect help



4:18 pm on Feb 26, 2013 (gmt 0)

I have over 8,000 Google URLs that I need to redirect

input URLs are like the following with unique cart_id and p_id

My code:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/store/agora.cgi
RewriteCond %{QUERY_STRING} cart_id=[^\&]+\&p_id=([^\&]+)
RewriteRule ^(.*)$ http://www.example.com/store/agora.cgi?user2=yes&p_id=$1&xm=on&ppinc=1a [R=301,L]

Output I get:

Output I want:

As you can see the variable $1 is being assigned "store/agora.cgi" instead of "16108".
I tried $2 but that is null. The Regular Expressions - User Guide tells me that $1 should be assigned 16108 because that is the first submatch in the regular expression ([^\&]+).

I'm confused and need help.


5:12 pm on Feb 26, 2013 (gmt 0)

I found it -of course only after posting for help

The error was $1 instead of %1 in the RewriteRule:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/store/agora.cgi
RewriteCond %{QUERY_STRING} cart_id=[^\&]+\&p_id=([^\&]+)
RewriteRule ^(.*)$ http://www.example.com/store/agora.cgi?user2=yes&p_id=%1&xm=on&ppinc=1a [R=301,L]


5:37 pm on Feb 26, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Move the REQUEST_URI pattern from the condition into the rule in place of (.*)

Ampersand does not need to be escaped.

Anchor the start of the QUERY_STRING pattern with (^|&) and adjust %1 to %2 in the rule.


6:56 pm on Feb 26, 2013 (gmt 0)

Thanks for the response.

What I have now works.
I think I tried putting the REQUEST_URI pattern in place of the (.*) But I will try it again now that I'm using % instead of $ for the variable.
Thanks for the (^|&) anchor, I was wondering about that.

Featured Threads

Hot Threads This Week

Hot Threads This Month