# Referrer redirect using .htaccess.



## support.clpafs (Apr 7, 2008)

I would appreciate some advice on setting up the .htaccess file to redirect end-users to a specific directory on my website when visiting from a 3rd-party website.

Example:

User clicks link from "Facebook" and is sent to my website "example.com", I would like the end-user to be redirected to "example.com/landing/" instead.


----------



## JiminSA (Dec 15, 2011)

It's pretty straightforward really, just set up a .htaccess file with a directory line like so

```
DirectoryIndex landing/index.html index.html index.php
```
this will make your default landing page = landing/index.html and if not found will use index.html in the root folder and if not found will use index.php in the root folder. 
(As you can see the urls quoted are all relative to the root folder).


----------



## JiminSA (Dec 15, 2011)

Incidentally, you may want to look at this article in Google Webmasters, regarding the specification of canonical URLs...


----------



## support.clpafs (Apr 7, 2008)

JiminSA,

Thank you for your reply, unfortunately I have an "index.php" in root folder, as well as a sub-directory "/landing/index.php" so I highlight doubt your first solution would work properly. However, I will take a look into the article that you've provided, and if I find a solution from your reference, I'll be sure to post it here.

Once again, thank you.


----------



## JiminSA (Dec 15, 2011)

No problem - just change the htaccess line I gave you, to this

```
DirectoryIndex landing/index.php index.html index.php
```
 it will look for the landing/index.php before looking for anything in the root folder. So that "solution" will work...


----------



## support.clpafs (Apr 7, 2008)

JiminSA,

As previously stated I believe that solution will not work, as I am looking to only redirect people who were sent to the site from a 3rd-party website such as Facebook or Google, and not direct traffic.


----------



## JiminSA (Dec 15, 2011)

> visiting from a 3rd-party website


Sorry, I missed the boat there 
I've am looking into how to recognise if a link into a site is direct from the browser or from a third party site - will let you know what I find...


----------



## Ent (Apr 11, 2009)

My suggestion would be to forget about .htaccess and write a splitter page in php. 
You may find $_SERVER['HTTP_REFERER'] is useful.


----------



## support.clpafs (Apr 7, 2008)

Ent,

I was looking toward that solution, however I would manually have to write the php coding for it or include it in every page on the website which is too much of an inconvenience, I was searching the web for an in-depth tutorial regarding .htaccess redirects based on referrer and have found that most use the following line of code:



> RewriteEngine On
> RewriteCond %{HTTP_REFERER}


However, there should be more to the "RewriteCond" followed by the "RewriteRule".


----------



## Ent (Apr 11, 2009)

I would strongly advise you not to redirect to the landing page for every hit that comes from a referrer. Often people will link to the specific page in your site that they're talking about, and that will be the most relevant page you could possibly send them to. Similarly search engines will point to the deep link, and may think that you're up to something suspicious if you show different pages.

If you really do need to do this, I'm afraid I can't suggest a pure htaccess solution. 
This kind of system with a single splitter script might work, but I haven't tested it.
I type in www.example.com/hello.htm
.htaccess redirects to splitter.php, passing hello.htm as a parameter.
splitter.php, noting that there is no referrer, sends you to hello.htm.

Or
I follow a link to www.example.com/hello.htm
.htaccess redirects to splitter.php, passing hello.htm as a parameter.
splitter.php, seeing that I followed a link from facebook, sends you to landing.htm.


----------



## support.clpafs (Apr 7, 2008)

Ent,

The idea is to redirect to a landing page "/landing" based on certain URLs, I have drafted a solution based on several articles online, and it appears to be doing exactly as I had planned.



> RewriteEngine on
> RewriteBase /
> 
> RewriteCond %{HTTP_REFERER} example\.com [NC,OR]
> ...


----------

