Celebrating 20 years online :)
Web Dev + WordPress + Security

Redirect All (Broken) Links from any Domain via HTAccess

Here’s the scene: you have been noticing a large number of 404 requests coming from a particular domain. You check it out and realize that the domain in question has a number of misdirected links to your site. The links may resemble legitimate URLs, but because of typographical errors, markup errors, or outdated references, they are broken, leading to nowhere on your site and producing a nice 404 error for every request. Ugh. Or, another painful scenario would be a single broken link on a highly popular site. For example, you may have one of your best posts mentioned in the SitePoint forums, but the person leaving the link completely botched the job:

(Read it here: http://domain.tld/path/popular-post/)

Ugh. Thanks for the hundred-thousand 404 errors, moron.

Fortunately, fixing either of these scenarios is relatively easy using a little HTAccess magic. All you need is an Apache-powered server with the powerful mod_rewrite module installed, and of course the ability to edit either your server configuration file or the root HTAccess file for your domain. Once you have that, here’s the code you need to redirect those pathetic broken links to the target of your choice:

<IfModule mod_rewrite.c>
 RewriteCond %{REQUEST_FILENAME} .*
 RewriteCond %{HTTP_REFERER} ^https?://([^.]+\.)?problem-domain\. [NC]
 RewriteRule (.*) http://redirected-domain.tld/target.html [R=301,L]
 # RewriteRule (.*) - [F,L]

Place this code into your root HTAccess file and edit the “problem-domain” and “http://redirected-domain.tld/target.html” with the problem domain and the redirect resource, respectively. The redirect resource may be anything — a web page, your home page, a different domain, a script, whatever. To deliver a “403 Forbidden” error for all such requests (instead of redirecting to an alternate resource), comment out the penultimate line and uncomment the last line. No other editing is required. Upload to your server and verify the results by clicking on one (or more) of the broken links on the problem domain. For the record, this method functions as follows:

  1. check for the required Apache module
  2. apply the rewrite to all file requests
  3. apply the rewrite to all requests from the problem domain
  4. perform the rewrite by redirecting all request to specified resource
  5. alternate rewrite rule for delivering a forbidden error message
  6. close the module-check container

I use this method all the time. Here is a working example that I recently removed because the site was taken offline:

<IfModule mod_rewrite.c>
 RewriteCond %{REQUEST_FILENAME} .*
 RewriteCond %{HTTP_REFERER} ^https?://([^.]+\.)?spinfeed\. [NC]
 RewriteRule (.*) http://domain.tld/note.txt [R=301,L]
 # RewriteRule (.*) - [F,L]

Apparently, the now defunct site, spinfeed.com had installed one of my free WordPress themes but didn’t bother editing the required code. My mistake was using URL examples from my own domain. Long story short, I was seeing frequent 404 errors resulting from the spinfeed.com site, and I wanted to resolve the issue. After several unsuccessful attempts at contacting the webmaster of the site, I eventually turned to HTAccess to deliver the message that dude needed to fix his links. So I uploaded the previous example and then created a file called “note.txt with the following message:

The Webmaster at spinfeed.com needs to update the current WordPress theme in order to resolve this issue.
Please contact me via https://perishablepress.com/contact/ for further assistance.
Thanks and regards,

This seemed like a good idea, although I never did hear anything from the spinfeed webmaster. Oh well, the site no longer exists, so the problem was solved nonetheless. :)

And that’s a wrap for this post. Keep in mind that the method described in this article will redirect all URL requests from a specific domain, not just the 404 requests. To redirect only a few broken links instead of everything, use Apache’s excellent Redirect directive instead:

Redirect 301 /blog/old-post-01/ http://domain.tld/new-post-01/
Redirect 301 /blog/old-post-02/ http://domain.tld/new-post-02/
Redirect 301 /blog/old-post-03/ http://domain.tld/new-post-03/

That will suit you much better when dealing with only a handful of broken links. Otherwise, if the problems are severe, just nuke ‘em with teh heavy stuff! :)

About the Author
Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
.htaccess made easy: Improve site performance and security.

7 responses to “Redirect All (Broken) Links from any Domain via HTAccess”

  1. question: what if I am the store/website owner who knows just enough to hurt myself? ex: I moved my domain, www.abeautifullife.com, to a new hosting environment and all is well, EXCEPT my old host has the homepage default thusly: www.abeautifullife.com/beautiful

    so now any of my 10,000 customers who go to their bookmark get a broken link!

    what to do? Is there an outside service that can redirect broken links in a ‘catch-all’ manner from a specified domain for a fee?

  2. Jeff Starr 2009/03/22 8:26 am

    Hi tony,

    Why not just redirect “http://www.abeautifullife.com/beautiful” to “http://www.abeautifullife.com”?

  3. I know this is an old post but the suggestions here come closest to something I am playing with in my head. I am wondering if I could use HTAccess to display another image whenever there is a broken image.

  4. It’s a great idea Cole, but not possible with HTAccess as far as I know. Definitely possible with PHP, Perl, or any other scripting/programming language. About the closest I have been able to get with such a method in HTAccess is to replace all 404 errors with a specific image. Perhaps a start..?

  5. Thanks for your reply. I will look into it with PHP. I just automatically think HTAccess for this sort of thing!

  6. Dallas Web Design 2010/01/21 6:26 pm

    awesome, just what i was looking for.

  7. Thank you so much; this worked like a charm!

Comments are closed for this post. Something to add? Let me know.
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
Blackhole Pro: Trap bad bots in a virtual black hole.
Crazy that we’re almost halfway thru 2024.
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Note to self: encrypting 500 GB of data on my iMac takes around 8 hours.
Getting back into things after a bit of a break. Currently 7° F outside. Chillz.
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.