The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 16, 2019, 01:30:24 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Need Help with Apache mod_rewrite  (Read 3030 times)
hvshah69
Rookie
**
Offline Offline

Posts: 21


View Profile
« on: April 01, 2009, 11:41:43 PM »


I would like the traffic coming to my site from ppc campaigns redirected to a given page say page1.php. The way I want to do is have a string say "ppc" in the destination URL of all my ppc campaign. In other words,  my destination URL on all the ppc campaign will have a format like this http://myurl.com/ppc

When the visitor arrives at page1.php through a ppc campaign and bookmarks it the bookmark should be http://myurl.com/page1.php. I can achieve that with "R" flag (force redirect). However, when the visitor tries to revisit the same page by using his bookmark, I want him redirected to a different site say http://myurl2.com. I am having a problem implementing this part. Here is my code

Code:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/ppc
RewriteRule (.*) /page1.php?id=$1 [L,R]
RewriteRule ^/page1 http://myurl2.com

Problem with the above code is that the visitor gets diverted to myurl2.com even when the ppc string is present in the URL. I was under the impression that "L" flag is supposed to prevent the second RewriteRule from executing when the RewriteCond is satisfied

What am I doing wrong? Angry
Logged
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #1 on: April 02, 2009, 05:59:14 AM »


I would like the traffic coming to my site from ppc campaigns redirected to a given page say page1.php. The way I want to do is have a string say "ppc" in the destination URL of all my ppc campaign. In other words,  my destination URL on all the ppc campaign will have a format like this http://myurl.com/ppc

When the visitor arrives at page1.php through a ppc campaign and bookmarks it the bookmark should be http://myurl.com/page1.php. I can achieve that with "R" flag (force redirect). However, when the visitor tries to revisit the same page by using his bookmark, I want him redirected to a different site say http://myurl2.com. I am having a problem implementing this part. Here is my code

Code:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/ppc
RewriteRule (.*) /page1.php?id=$1 [L,R]
RewriteRule ^/page1 http://myurl2.com

Problem with the above code is that the visitor gets diverted to myurl2.com even when the ppc string is present in the URL. I was under the impression that "L" flag is supposed to prevent the second RewriteRule from executing when the RewriteCond is satisfied

What am I doing wrong? Angry

The L flag does mean do not read more, but then, whatever you've already done
is sent to the visitor's browser and his browser comes back with a different URL.

In your case, it would come back for page1 and be sent to http://myurl2.com

I am not 100% sure that I understand the effect you want, but you could try adding
another condition which is negated with the !.

Code:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/ppc
RewriteRule (.*) /page1.php?id=$1 [L,R]

RewriteCond %{REQUEST_URI} !^/ppc
RewriteRule ^/page1 http://myurl2.com

But then *everyone* without ppc will go to myurl2.com

If you must only do this ONLY with people that have visited previously
you might need to use cookies, I don't know for sure.

Let's see who else replies.

Bompa
« Last Edit: April 02, 2009, 06:02:25 AM by Bompa » Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #2 on: April 02, 2009, 06:09:42 AM »

You'd need to do a conditional 301.

Incidentally, it's a lot easier to just rewrite every URL to a single script that reads the REQUEST_URI and does actions based on that, imo.
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #3 on: April 02, 2009, 10:34:39 AM »

Bomps is spot on, as is VS IMO.

It looks to me like you're starting down a path of more complicated logic than you'd want to put into Apache. Although the mod_rewrite engine is a friggin' swiss army knife, it may get really ugly and difficult to debug if you rely on it for complicated logic that includes first visit, revisit etc. It can be done, but it would not be pretty IMO.

So I think VS is right: get into your language of choice via rewriting ALL urls into a single script, and go from there. Once you're into, say, PHP, you can easily look for cookies, assess the inbound URL with a little more calm and debugging ability. The overhead will be negligible if you have a tiny script that does nothing but the URL translation, then includes actuary code if it must ... or use APC to make sure that your script is always compiled and it will really fly.

The mod_rewrite therefore becomes really simple:

RewriteRule ^(.*)$  /(myphpscripts location)/main.php?originalurl=$1  [L]

whatever was passed in the original URL will now be in $_GET['originalurl'] ... add salt and pepper to taste. main.php then would require/include or simply echo file_get_contents($originalurl) once it decides what it needs to send back.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
hvshah69
Rookie
**
Offline Offline

Posts: 21


View Profile
« Reply #4 on: April 05, 2009, 11:18:04 PM »

Thanks for all the replies but I still cannot get it to work the way I want.

I have concluded that mod_rewrite is hopeless as far as if else type logic is concerned. So I was trying to focus using php combined with mod_rewrite as perkiset recommended. However, here is my problem:

  • user clicks on my banner ad
  • all the traffic is directed to a php script using mod_rewrite with the url path as the argument
  • since the R-flag is not used the url in the browser does not change (I think it does not matter either way)
  • the script takes appropriate action and echoes the information I want
  • if the user decides to bookmark the page and revisits the page later, how do I show him different content than the previous case?


In other words
PPC traffic = Content A
Non-PPC traffic (including bookmarked Content A page) = Content B

Should I be looking at creating unique session ids for each visitor? or is there a simpler way?


Logged
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #5 on: April 06, 2009, 07:03:54 AM »

Think about the difference between a user visiting your site for the first time vs. subsequent times. What can you do to make sure you know who he is and that he's visited you before? I'm hoping the answer is obvious to you if you have read this far but if not I will spell it out: Drop a cookie on him to mark him as having come from paid search to your page, make it have a really long expiration time. Most people don't clear their cookies often. Check for that cookie, make it something simple like "visited = 1".

Fool proof? Not nearly, but it would probably work fine for what you need to do.
Logged

hai
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #6 on: April 06, 2009, 07:50:17 AM »

I have concluded that mod_rewrite is hopeless as far as if else type logic is concerned.
Actually, mod_rewrite is enormously powerful, even in an if/then situation, provided it's the right tool for the job. Don't give up on it as a tool in your toolbox. What you are doing could be executed with mod_rewrite, but it'd be ugly.

VS is 100% right, both in trying to get you to consider the problem and the solution. The real essence of the challenge is not an if/then: it's understanding that this is your surfers nth time visiting. The challenge is to "mark him" so that you'll know in the future.

@ Session ids: sessions are built to expire in PHP. You only want a session to live 20 minutes, or an hour or something. They are not the correct tool for this job.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!