Save 25% on Wizard’s SQL for WP w/ code: WIZARDSQL
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, 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 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 needs to update the current WordPress theme in order to resolve this issue.
Please contact me via 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! :)

Jeff Starr
About the Author
Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
The Tao of WordPress: Master the art of WordPress.

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,, to a new hosting environment and all is well, EXCEPT my old host has the homepage default thusly:

    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. Avatar photo
    Jeff Starr 2009/03/22 8:26 am

    Hi tony,

    Why not just redirect “” to “”?

  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. Avatar photo

    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.
Daylight savings is a complete waste of time and needs to be eliminated.
Got a couple of snow days here in mid-March. Fortunately it's not sticking.
I handle all email in real time as it comes in, perpetually clear inbox for years now.
Added some nice features to Wutsearch search engine launchpad. Now 21 engines!
.wp TLD plz :)
Nice collection of free SEO APIs and user-agent lookups for Googlebot, Bingbot, Applebot, YandexBot, and more.
90% of online customer support is just explaining how to do basic troubleshooting.
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.