Jason
Jason

Reputation: 542

Rewrite requests to website from specific domain name with .htaccess

How do I rewrite all requests from a specific domain name to my site with .htaccess?

For example, if http://www.example.com had a link to my site, then I want to redirect them to a different page on my site, but other links from other websites and direct links should display the requested page like normal.

I've been getting in a muddle with how to do this... Thanks in advance!

UPDATE:

Some information as requested in an answer:

My stripped down .htaccess file looks like this:

# irrelevant all squashed together (issue still remains when used like this)
 <Files .htaccess>
  order allow,deny
  deny from all
 </Files>
Options +FollowSymLinks
RewriteEngine On
RewriteOptions MaxRedirects=10
DirectoryIndex index.php
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301]
ErrorDocument 400 /error.html
ErrorDocument 401 /error.html
ErrorDocument 403 /error.html
ErrorDocument 404 /error.html
ErrorDocument 500 /error.html
Options All -Indexes
IndexIgnore *
LimitRequestBody 10240000

#redirect
 RewriteCond %{HTTP_REFERER} ^http://(www\.)?badsite\.com/ [NC] 
 RewriteRule !^error\.html /error.html [L,NC,R=302]

The header of the page I am visiting looks like this:

Referer: http://www.badsite.com/wiki/Page

2nd UPDATE:

The intended actions are:

  1. Visit http://www.example.com/wiki/Page
  2. Click on link to http://www.mysite.co.uk/thispage/here
  3. Because of the example.com, I should be redirected to http://www.mysite.co.uk/badpage instead of http://www.mysite.co.uk/thispage/here like all other requests.

Upvotes: 2

Views: 2529

Answers (1)

anubhava
anubhava

Reputation: 785266

You will need to rely on HTTP_REFERER for this. Put this code in your sub-directory's .htaccess file:

RewriteEngine On

RewriteCond %{QUERY_STRING} !^id=[^&]+ [NC]
# if referrer is bad.com
RewriteCond %{HTTP_REFERER} (www\.)?bad\.com [NC]
# then redirect to a different page
RewriteRule !^start start [L,NC,R=302]

Though remember that HTTP_REFERER can be spoofed and is not 100% reliable.

PS: It turned out to be unique situtation due to dokuwiki installation

Upvotes: 1

Related Questions