2

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.
anubhava
  • 761,203
  • 64
  • 569
  • 643
Jason
  • 542
  • 5
  • 24

1 Answers1

1

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

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • That is very similar, maybe even the same to what I tried, but it does not work. I don't know why. There must be something odd going on with my .htaccess file but I'm having trouble working out what. – Jason Nov 04 '13 at 17:57
  • You should post your complete .htaccess in your question for investigation. – anubhava Nov 04 '13 at 18:00
  • Done that, could you possibly take a look and see if there is anything glaringly obvious wrong? – Jason Nov 04 '13 at 18:13
  • For some unknown reason, this still does not work. I have never had an issue with .htaccess in the past. I have allowed access to the .htaccess file now and I visited `http://www.mysite.co.uk/.htaccess` and it shows the same code as you have written. – Jason Nov 04 '13 at 18:34
  • Is `.htaccess` even enabled? Try putting some garbage text on top and see if you get 500 error OR not. – anubhava Nov 04 '13 at 18:44
  • Yep that works fine. All other rules I have (like external and internal redirections for 'clean' URL's) work fine. It is odd that this does not work, especially as the header matches the URL. I am of course changing the domain to the correct one, but the theory should still work and I really don't understand why it doesn't. – Jason Nov 04 '13 at 18:54
  • With a changed condition, can you try: `RewriteCond %{HTTP_REFERER} (www\.)?badsite\.com [NC]` – anubhava Nov 04 '13 at 18:58
  • That doesn't work either. Is there any other HTTP header variables that we could include that we might maybe have more luck with? I can't think of any, but you seem more knowledgeable than I am! Also, do you know if theres any way of disabling what we are trying to do? Because if there is, we can try and enable it, but again I don't know anything about this. – Jason Nov 04 '13 at 19:03
  • Unfortunately other than HTTP_REFERER there is not other header variable that can tell you whether link is coming from a bad site. – anubhava Nov 04 '13 at 19:07
  • I can't think of anything we can do then. How odd :/ – Jason Nov 04 '13 at 19:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40525/discussion-between-anubhava-and-jason) – anubhava Nov 04 '13 at 19:08