Solved

How to redirect based on the visitor's domain

Posted on 2006-06-09
4
301 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
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CentOS 7 Installation 7 72
Penetration Testing home based work 3 73
How code a 301 redirect for folder files -> 1 file 2 39
000webhost.com default error log 1 23
Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now