Solved

How to redirect based on the visitor's domain

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.
Suggested Courses

626 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