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.

1 Answer 1

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

11
  • 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 4, 2013 at 17:57
  • You should post your complete .htaccess in your question for investigation.
    – anubhava
    Nov 4, 2013 at 18:00
  • Done that, could you possibly take a look and see if there is anything glaringly obvious wrong?
    – Jason
    Nov 4, 2013 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 4, 2013 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 4, 2013 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.