Constraint
Constraint

Reputation: 33

Redirecting all traffic based on Host name

An external server (I'll call it "sub.origin.edu") redirects all traffic to my webpage. I want to take all traffic from this host, and redirect it to a different site (which I'll call "http://foo.target.edu/board/").

My .htaccess file is:

 RewriteEngine On
 RewriteCond     ${HTTP_HOST}    sub\.origin\.edu [NC]
 RewriteRule     ^(.*)$  http://foo.target.edu/board/ [R=302]

This doesn't seem to be working. I've confirmed (using PHP) that the host is indeed sub.origin.edu, and the .htaccess file is in the right directory, but this rule just doesn't come into effect. Any suggestions? Thanks.

(If I remove the RewriteCond, the redirect happens, so I can confirm that everything but the rewrite condition is working.)

Upvotes: 1

Views: 9874

Answers (3)

dianaAvila
dianaAvila

Reputation: 106

I found this question while trying to complete a re-derict for specific hostnames. This link was of great help to understand how RewriteCond and RewriteRule work.

http://httpd.apache.org/docs/2.4/rewrite/intro.html

Upvotes: 1

Femi
Femi

Reputation: 64700

Use this:

RewriteEngine On
RewriteCond     %{HTTP_HOST}   sub\.origin\.edu [NC]
RewriteRule     ^(.*)$  http://foo.target.edu/board$1 [R=302]

You used the wrong substition character ($ instead of %)

Upvotes: 9

Chris S
Chris S

Reputation: 785

If sub.origin.edu is doing a 3xx redirect, then the browser will issue a new request to your server using your.server.edu as the host name. So this rule will never match that. If this is the case, there's no easy way to tell where the request was redirected from.

If they're using a CNAME, Femi has the correct answer.

Upvotes: 0

Related Questions