Knowledgebase

How to redirect links from particular websites

With the help of mod_rewrite you can redirect links from any external site pointing to content on your site. You can redirect those links to content on the same hosting account or you can redirect them to an external URL.

What you need to do to achieve this is to place a few Apache directives in the .htaccess file that's in the public_html directory on your hosting account. Of course, you can also use an .htaccess file that's in a subfolder provided that the content to be redirected is in the same subfolder.

For example, if you want to redirect all links to content on your hosting account that come from the site exampledomain.com, you can use directives like these ones:

#Redirect requests coming from exampledomain.com to anotherdomain.com
RewriteEngine On
RewriteCond %{HTTP_REFERER} exampledomain\.com
RewriteRule (.*) http://anotherdomain.com [R]

With the above directives requests to yourdomain.com from the site exampledomain.com will be redirected to anotherdomain.com. For example, a link on exampledomain.com that points to yourdomain.com/index.php will be redirected to anotherdomain.com/index.php. Links from sites other than exampledomain.com will not be redirected.

Instead of redirecting to an external URL you can also redirect the content to another location within the same hosting account. For example:

#Requests coming from exampledomain.com for content within
#yourdomain.com/images are redirected to yourdomain.com/folder/pictures

RewriteEngine On
RewriteCond %{HTTP_REFERER} exampledomain\.com
RewriteRule ^images/(.*) /folder/pictures/$1 [R,L]

In the above case links on the site exampledomain.com for files within yourdomain.com/images will be redirected to yourdomain.com/folder/pictures.

Of course, when using the above example rules you need to change the paths and URL addresses with the actual ones that you want to use. For general information about the mod_rewrite directives check out the tutorial on mod_rewrite.

In case you want to block links from some websites you may find useful the article on blocking other websites from accessing your site.

Was this answer helpful?

 Print this Article

Also Read