Forum Moderators: phranque

Message Too Old, No Replies

Not rewritting correctly

Trying to redirect requests to an affiliate link

         

erikcw

6:16 pm on Mar 11, 2004 (gmt 0)

10+ Year Member



Hi All!

I've been playing around with this for a few days, and haven't been able to get it to work right. I'm part of an affiliate program, and want to mask my links with mod-rewrite to ensure that my ID number stays in the link...
Here is my .htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule shoppingcart\/(.*)$ http://www.myaffiliateprogram.com/u/connect/t.asp?id=8905&p=$1

The link in my html is to
http://www.example.net/shoppingcart/product.asp?id=E206
(product.asp?id=E206 is the product page on the affiliate program site I want to end up at)

Unfortunatly my mod-rewrite is taking me to the affiliates main page, and not the page I am requesting. However if I manually enter the URL in my browser http://www.myaffiliateprogram.com/u/connect/t.asp?id=8905&p=product.asp?id=E206
it takes me exactly where I need to go.

Any ideas on what I may be doing wrong?

Thanks!

[edited by: jdMorgan at 4:00 am (utc) on Mar. 15, 2004]
[edit reason] Examplified URL, de-linked [/edit]

Birdman

7:34 pm on Mar 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try NOT escaping the slash in the pattern. Also, I don't think you need the rewrite base.

RewriteRule ^shoppingcart/(.*)$

Birdman

erikcw

10:20 pm on Mar 11, 2004 (gmt 0)

10+ Year Member



Birdman,

I tried what you suggested, but I am still getting the same result. Any other ideas?

Birdman

11:00 pm on Mar 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, Is see the prob. But first, you need to edit out your personal URLs because it's against the rules here. It's best if you do it, rather than making a moderator take the time to do it.

Back to the prob at hand. What's happening is the regular expression(.*) is not catching the querystring in the rewriterule. You need to use a rewritecond to get the query string. You use %1, instead of $1 to reference that variable.

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond {QUERY_STRING} ^(.*)$
RewriteRule ^shoppingcart/(.*)$ h*tp://www.example.com/u/connect/t.asp?id=8905&p=$1?%1 [L]

Notes:

[webmasterworld.com...] (similar to your prob)

Notice how I examplified your URL and delinked it. The mods don't want people linking to their sites and they don't want a bunch of dead example URLs. I'm not trying to give you a hard time ;)

erikcw

11:32 pm on Mar 11, 2004 (gmt 0)

10+ Year Member



I'll be more carefull with URLs in my posts...

I tried using the code you provide and
a link to shoppingcart/product.asp?id=E206
outputted
http;//www.examplaffiliatesite.com/product.asp%253f%257bQUERY_STRING%257d

it should be sending it to example.com/product.asp?id=E206.

Any ideas?

Thanks!

Birdman

11:50 pm on Mar 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops, I left the % out before {QUERY_STRING}. Give that one a go. I'm sure our inhouse guru, jd_morgan, will stop by and get this straightened out if we don't.

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^shoppingcart/(.*)$ h*tp://www.example.com/u/connect/t.asp?id=8905&p=$1?%1 [L]

erikcw

12:02 am on Mar 12, 2004 (gmt 0)

10+ Year Member



Birdman,

Slightly different results, but still not what we are looking for.
The link is now redirecting to
ht*p://example.com/product.asp%253fid%3DE206 [note a slightly different string at the end] (it looks like it is getting E206 right at the end, but it is adding in %253f instead of? and %3D inseat of =)

Sensitive stuff!

[edited by: erikcw at 12:54 am (utc) on Mar. 12, 2004]

Birdman

12:11 am on Mar 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it's tough sometimes :) I'm no genius at it either.

I think it's because we're trying to capture the?id= part. Maybe we need to just capture the E206 and hardcode the id= part.

RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^shoppingcart/(.*)$ h*tp://www.example.com/u/connect/t.asp?id=8905&p=$1?id=%1 [L]

If that one doesn't work, I think it may be because of the two (?) delimiters. Only the first one should be a (?). All the rest should be (&).

BTW You can edit your posts by clicking the "Owner Edit" link under your username.

erikcw

12:53 am on Mar 12, 2004 (gmt 0)

10+ Year Member



I'm still getting the same thing.
product.asp%253fid%3DE206

Perhaps I should be \escaping parts of the RewriteRule?
could another rewrite rule I have on the next line be doing something:
RewriteRule (.*)\.html$ $1.php

Thanks

Birdman

2:45 am on Mar 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tested it and it's definately the second question mark(?) in the rewritten URL. It needs to be an ampersand(&).

erikcw

4:48 am on Mar 12, 2004 (gmt 0)

10+ Year Member



I can't remove the second (?) becuase it is hard coded into the affiliate link.

everything after the first (?) is for the affiliate program management site. Everything after the second? is for the merchants dynamic web page. If I remove the second (?), their site doesn't know what to do with my request.

Are there any other possible solution? I tried escaping the second?, but that didn't work....

closed

5:19 am on Mar 12, 2004 (gmt 0)

10+ Year Member



Have you looked at maybe using the
NE
flag for the RewriteRule yet?

erikcw

6:38 am on Mar 12, 2004 (gmt 0)

10+ Year Member



Closed, that NE flag did the trick! Thank you both Birdman and Closed for your help!

Cheers!

closed

7:11 am on Mar 12, 2004 (gmt 0)

10+ Year Member



You're welcome, erikcw!

gethan

9:58 am on Mar 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A bit late but I think:

RewriteRule ^shop/(.*)$ [example.com...] [L,QSA]

Would also work,

QSA = Query String Append.

Birdman - I didn't spot the query string either on first reading.

NE = not used it off to read the manual ;)

[added]Yep that NE No-Escape is still needed so

RewriteRule ^shop/(.*)$ [example.com...] [L,QSA,NE]

I don't think it would work, but nice example for future though.
[/added]