dkeeper09
dkeeper09

Reputation: 547

Keep htaccess redirect on entire site

I am going to do my best to explain this. I have an affiliate site being setup. So they can go sign up, then the username they provide is written to the database. They can then access their affiliate site at www.domain.com/affiliate/username.

I have my .htaccess file redirecting the url: www.domain.com/affiliate/home.php?affiliate=username to www.domain.com/affiliate/username

I hope that makes sense. What I need is how to keep that username variable constant on the entire site. So if they are on the homepage of domain.com/affiliate/username and they click on the About page or something else, I want to make sure it stays at domain.com/affiliate/username. That way when the person goes to buy something, I make sure it is bought through the affiliate site, not the regular domain.com site.

Any help is greatly appreciated. I am new to this, so bear with me please. Thanks!

Upvotes: 0

Views: 56

Answers (1)

IMSoP
IMSoP

Reputation: 98005

3 approaches, quickly summarised, none of which have anything to do with mod_rewrite / .htaccess (making this an example of what I just learned is called the XY Problem):

  • Set a cookie with the affiliate they came from. Ignore the URL once that cookie is set, and credit the booking to the appropriate affiliate. (If you're using a PHP session already, you can stick the affiliate in there, which comes to much the same thing.)
  • Rewrite all your URLs in your PHP code so that whenever you display a link or form, it pushes to the appropriate URL. Much harder to write and easier to introduce bugs.
  • Use sub-domains rather than sub-directories - e.g. affiliate42.example.com - and make all your links base-relative. One stumbling-block is that you'll need a wildcard SSL certificate which covers all the sub-domains, which is a bit more expensive.

If you really really wanted to do this with Rewrite Rules, you could probably combine it with the cookie option somehow: based on the cookie, redirect them differently. But I'm not sure there's much advantage to that, and forms that submit by POST will probably still need to be handled in the PHP layer as those are tricky to redirect consistently.

Upvotes: 1

Related Questions