Solved

How to redirect based on the visitor's domain

Posted on 2006-06-09
4
303 Views
Last Modified: 2013-12-16
Hello all,

If I want to force visitors of my website from a certain domain but allow other visitors to browse my site as ever, how do I go about it?

My website is running on a linux server and I do have .htaccess and RewriteRules available. If a user enters URL http://www.mysite.com/ and is from a certain domain, it should be redirected to http://www.mysite.com/certainfolder, otherwise do nothing.

My .htaccess looks like this:

RewriteEngine on
# If the host is www.certaindomain.com
RewriteCond %{REMOTE_HOST} ^www\.certaindomain\.com$
# redirect to http://www.mysite.com/certainfolder
RewriteRule ^.*$ http://www.mysite.com/certainfolder

But the following seems to affect all users no matter what. I need the visitors from certain domain to automatically be forwared to http://www.mysite.com/certainfolder while others to http://www.mysite.com/

Thanks for your help!
0
Comment
Question by:agajewski
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 22

Expert Comment

by:pjedmond
ID: 16871380
REMOTE_HOST will always be the domain of the HTTP request...hence it acts on all requests to that domain:)  (Realistically it's used a virtual host rewriting role). You actually need HTTP_REFERER - this gives the host that the browser has come from:)

This is a little more complex than a standard rewrite if you need to have multiple rules, in that it is easier to use a rewrite map. An example of how to do that is here:

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

under Referer-based Deflector

-------------8X----------------------------------------------------
Referer-based Deflector
    RewriteMap  deflector txt:/path/to/deflector.map

    RewriteCond %{HTTP_REFERER} ^www\.certaindomain\.com$
    RewriteCond ${deflector:%{HTTP_REFERER}} ^-$
    RewriteRule ^.* %{HTTP_REFERER} [R,L]

    ... in conjunction with a corresponding rewrite map:

    ##
    ##  deflector.map
    ##

    http://www.mysite.com/index.html    http://somewhere.com/certainfolder/

-------------8X----------------------------------------------------
HTTP_REFERER is the address that the browser has come from to get to you.

Above should work nicely, but will need adjusting to your environment. Yo may find that as you have already done, replacing REMOTE_HOST with HTTP_REFERER is sufficient for your needs:)

HTH:)
0
 
LVL 22

Accepted Solution

by:
pjedmond earned 125 total points
ID: 16871424
Thinking about this a bit more for .htaccess - ignore the rewrite map approach, and just use:

RewriteEngine on
# If the host is www.certaindomain.com
RewriteCond %{HTTP_REFERER} ^www\.certaindomain\.com$
# redirect to http://www.mysite.com/certainfolder
RewriteRule ^.*$ http://www.mysite.com/certainfolder

You don't want additional files around, unless you really need them!
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question