0

How to redirect a first time user to a special landing page using htaccess based on referrer? I mean if they came from another domain then they are the first time visitor?

I am really noob at url rewriting and explanation would be great .

Note: the landing page is nothing but a php script that detects browser. On that page I will use cookie, but need to redirect the user if the referrer is empty or its from another domain.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
user80287
  • 560
  • 3
  • 11
  • 17

3 Answers3

1

I suggest this :

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^(www\.)?(https?://)?example\.com[NC] 
RewriteCond %{REQUEST_URI} !^/welcome.html [NC] 
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,L]

The first RewriteCond check if referer contains your domain name, and the second check if you are not just redirected by the RewriteRule. The RewriteRule brings you to the welcome page as a [L]ast rule.

error500
  • 159
  • 2
  • 10
  • While this might be a helpful answer (I personally don't know), adding a bit of explanation would improve its usefulness considerably. – djikay Jul 20 '14 at 10:01
  • Not working for me. The original redirect works, but once I'm at the domain and try to visit another page I still get redirected back. – norsewulf Aug 11 '15 at 17:00
0

Excuse my reheating the old steak once more.. I would still be interested in knowing if anyone knows the solution to this problem - without using cookies or HTML5 features...

I have read here that the HTTP_REFERER might be blank. Is that why this method of redirecting is not good for this application? I have experimented with this on my server but the closest result working result was being always redirected to my landing page index.htm, which is not desired..

Could this rule interfere with other rewrite rules?

Also, there is an error in the former snippet:

And I think the NC flag in the latter snippet does not make sense. Should it not be L?

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^(www\.)?(https?://)?example\.com[NC] 
#missing space after .com and before [----------------here----^
RewriteCond %{REQUEST_URI} !^/welcome.html [NC] 
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,L]


RewriteEngine on
RewriteCond %{HTTP_REFERER} ^(www\.)?(https?://)?(?!example\.com) [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,NC]
#Should this flag not be L? ------------------------------^
  • Hi @wiktoria, I think this question needs a new post for itself. You're unlike to have answers on a very old question like this one. – Marc Brillault Oct 01 '18 at 22:28
0

How about redirect the use if his referer is not your domain ?

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^(www\.)?(https?://)?(?!example\.com) [NC]
Rewriterule ^(.*)$ http://example.com/welcome.html [r=307,NC]

That means that the user will be redirected to welcome.html if he writes example.com in the address bar or comes from a link in another site. Once on your site it won't be redirected anymore if he load another page in your site.

P.S. AFAIK you can use cookies in PHP that generates a plain html page see here

Edit: Update tested code

Radu Maris
  • 5,648
  • 4
  • 39
  • 54
  • @jasonaburton - Updated code, It still has a problem, it will match any characther after the domain regex: "example\.com.", I'm short on time now, if somebody could fix this or I will come back later. – Radu Maris Jul 19 '12 at 09:39