homepage Welcome to WebmasterWorld Guest from 54.211.97.242
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
htaccess 301 redirect help
griz44



 
Msg#: 4549009 posted 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
http://www.example.com/store/agora.cgi?cart_id=6497903.234477*8X3D96&p_id=16108&xm=on&ppinc=1a

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:
http://www.example.com/store/agora.cgi?user2=yes&p_id=store/agora.cgi&xm=on&ppinc=1a

Output I want:
http://www.example.com/store/agora.cgi?user2=yes&p_id=16108&xm=on&ppinc=1a

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.

 

griz44



 
Msg#: 4549009 posted 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]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4549009 posted 5:37 pm on Feb 26, 2013 (gmt 0)

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.

griz44



 
Msg#: 4549009 posted 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved