Solved

How to redirect based on the visitor's domain

Posted on 2006-06-09
4
299 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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

743 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

16 Experts available now in Live!

Get 1:1 Help Now