Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to redirect based on the visitor's domain

Posted on 2006-06-09
4
Medium Priority
?
307 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
2 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 500 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
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…
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…
Suggested Courses
Course of the Month7 days, 8 hours left to enroll

824 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