Redirect user based on originating URL

299 Views Asked by At

I need to redirect traffic to a URL based on it's originating URL.

· siteabc.co.uk domain will redirect to sitexyz.co.uk/camps

· siteabc.co.uk/daycamps will also exist

What I would like to happen is if a customer types in siteabc.co.uk they will be redirected to sitexyz.co.uk/camps where a pop up/lightbox will come up to say ‘Are you looking for Daycamps?’ if clicked the user will be taken to siteabc.co.uk/daycamps

If they land directly on sitexyz.co.uk/camps the pop up should not be shown.

I need the lightbox to only be shown if they have arrived from siteabc.co.uk, is this possible?

I'm thinking jQuery should be able to do it, but I'm not sure if there is a starting point to use. Any help appreciated.

4

There are 4 best solutions below

0
On BEST ANSWER

If you are working on any Apache Web server then the solution to your problem is to use .htaccess file. htaccess files(distributed configuration files) provide a way to make configuration changes on a per-directory basis.

First create a file with name .htaccess and keep it in the root directory. in your case at siteabc.co.uk/.htaccess

Because different versions of Apache server the configuration file syntax may changes, here are the codes that should work

Type: 1

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !siteabc.co.uk/$ [NC]
RewriteRule ^(.*)$ siteabc.co.uk/camps/$1 [L,R=301]

Type: 2

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) siteabc.co.uk/camps [R=301,L]


Type: 3

RewriteEngine On
RewriteRule ^$ /camps [L]



Thanks

0
On

If you want to do it only by JavaScript then use document.referrer to get Referer URL. It will give you from where you have redirected to that page. But it is always better to rely on Server Side Scripting.

0
On

This is more a server side thing as you can easily see the referring URL, your best bet is to use what ever code you are using to add a class based on where it came from, if you show your code I can help more, but until then add a class based on server request, NodeJS is brilliant at this but equally so it PHP. It is also more accurate in most cases.

0
On

You may use a URL parameter to decide what to show. Consider the following simple example.

redirect1.html

<!DOCTYPE html>
<html>
<head>
<script>
window.onload = function () {
    window.location.assign('redirect2.html?redirect1');
};
</script>
</head>
<body>
</body>
</html>

redirect2.html (the same dir)

<!DOCTYPE html>
<html>
<head>
<script>
window.onload = function () {
    var paramString = window.location.href.split('?')[1];
    if (paramString !== undefined) {
        console.log(paramString);
    }
};
</script>
</head>
<body>
</body>
</html>

JS in the 1st file redirects the page to the 2nd file (you may feed the location.assign function with a URL too). JS in the 2nd file parses its own URL and prints the part after '?' to your browser console. Instead you may decide what to do depending on what is there after '?' (domain name, for example).

P.S. This reference may be handy.