Help With htaccess redirect?

6 replies
Can anyone help me understand, in layman's terms, how to create link cloaking using htaccess redirect for affiliate links? I've read a bit about, and attempted, however, can't get it to work. Need to add extensions to my web site URL which redirect to affiliate program.

Thank you in advance for any help.
#htaccess #redirect
  • Profile picture of the author n7 Studios
    Hi Barry,

    Don't worry - htaccess files and their RewriteRules (the rules that "cloak" URL's) are quite complex to get your head around at first. I still get stuck from time to time, and I've been using them quite a lot!

    Anyway, to answer your question, you'd need to create a file called .htaccess, and store it on the public html root folder on your web server i.e. the FTP folder where you would upload your web site to.

    In the .htaccess file, you can use the following code
    PHP Code:
    RewriteEngine On
    ^(.*)$1 [L] would forward to would forward to

    Of course, I'm not exactly sure what URL's you currently have, and where you wish to forward them to. Feel free to drop me a PM with further details, and I can provide a more specific htaccess file, and assist setting it up on your server, if needed.
    {{ DiscussionBoard.errors[361861].message }}
    • Profile picture of the author Barry Rice
      Thank you so much n7 Studios. I'm quite the novice with any kind of programming/technical stuff, so any help appreciated. I'll tinker with, see if I can get to work, and if not, will PM you. Thanks for the kind offer.
      {{ DiscussionBoard.errors[364332].message }}
  • I take a much different approach but it's also a lot more technically advanced (Although still very simple)

    You can use the htaccess to push through a simple php redirect so that you can log your clicks and also control your affiliate redirects using a database. This is probably a bit out of the realm for how simple you want to keep things but I like to show people the potential options available.

    Here's what I do:

    PHP Code:
    RewriteEngine on
    Rewrite Rule 
    goto/([^/]+)/$  /redirect.php?offer=$1
    Rewrite Rule 
    goto/([^/]+)/([^/]+)/$  /redirect.php?offer=$1&subid=$
    This htaccess rewrites a friendly link to your redirect so you can have a like like:

    Now it goes to

    The simple way to write your redirect.php would be like this:

    if ($_GET['offer']=='A-Really-Cool-Ebook')
    header('Location: http://yourhoplinkhere');

    if ($_GET['offer']=='Some Other Offer')
    header('Location: http://yourotherhoplink);

    With this structure in place you can realize more complicated possibilities. You can store your hoplinks and offer names in a database, then you can change the hoplinks for certain offers across your entire site should you need to change the link.

    You can also log each click and information about the user before you redirect them. For instance you can pull down their IP address, what browser they're using, and the exact time they clicked. I use this data to cross reference with my own analytics that I track server side so I can see the path that each person took on my site.

    These are all complicated things you would need assistance with setting up. A good coder would be able to understand this post and build exactly what I just described. Although I already have all this built, I'm not taking on any new work until my current contracts are finished. I just thought I'd lend a hand in finding the best direction possible

    Happy New Year!
    {{ DiscussionBoard.errors[365260].message }}
    • Profile picture of the author Barry Rice
      Thank you for taking the time to respond. Quite advanced, but certainly something to work toward as I see the benefit of collecting more info on who's clicking.
      {{ DiscussionBoard.errors[367066].message }}
  • Profile picture of the author rarebiz
    You can also use "Redirect" menu from CPanel.
    It makes folder and generates .htaccess file

    For link cloacking i prefer using simple php:
    make a folder /public_html/offer1

    put this index.php there
    header("HTTP/1.1 301 Moved Permanently");

    Final cloacked link is
    {{ DiscussionBoard.errors[367684].message }}
  • Profile picture of the author zeroanarchy
    Hi I am new and I thought I would start off by asking a question relating to this post as I can see there are a few people here who understand regular expressions.

    If I have files that reside in folders as follows.



    How could I rewrite the URL to look like the below examples without having to chang the source location of the file.



    bearing in mind that the folder that contains the numeric alpha mix may vary look different for each pdf.

    Any ideas?

    {{ DiscussionBoard.errors[373413].message }}

Trending Topics