Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Users on my site are given unique URL's for me to then track how many people they have referred to my site.

IE: http://www.example.com/FQ3DL (FQ3DL being the unique code/url)

The first thing I'd like to do is when a user goes to that link, it displays the homepage http://www.example.com/ rather than a 404 error

The second thing is, how would I track how many people have visited that URL? Only through Google Analytics or is there another way to manage it?

Thanks!

share|improve this question
add comment

1 Answer

I would probably write the code myself to track them. Although perhaps it would be easier trying to hook it into analytics. I'm not sure how to do that.

To do the re-direct you would want to use .htaccess (providing you're using Apache server) The .htacess file might look something like this:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([A-Z0-9]{5})$ track.php?id=$1 [L]

The RewriteRule there will match someone going to example.com/HJK7D Basically example.com/ and any combination of 5 capital letters and or numbers. This will get re-written and the server will actually pull up track.php?id=HJK7D (in this case)

So drop that .htaccess into the main root directory of your website. Then create a file called track.php which might look something like this:

<?php
    if(isset($_GET['id'] && strlen($_GET['id'])==5){
        setcookie("id", $_GET['id'], time()+1209600); // expires in 2 weeks
    }

    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.example.com/");
?>

Then use the cookie $_COOKIE['id'] to track your visitor through your site.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.