WordPress Re-directing ?attachment_id=xx To An Alternative URL


About The Author
Cabe Nolan is the founder of WP Cover where he shares his insight into WordPress, development, & entrepreneurship. Outside of WPCover, Cabe continues to run a successful WordPress development firm, Bold City Design as well as a few high profile websites, Arrivala, Two Way Resume, Dock Skipper, and a successful outdoors brand, DolfinPack.







WordPress Re-directing ?attachment_id=xx To An Alternative URL

A couple days ago I posted a quick code snippet for redirecting index.html to the WordPress root domain and avoiding the hated redirect loop error. Today, I wanted to share another quick snippet that I’ve needed quite a bit. In certain cases, Google may index an attachment in the form of www.yourdomain.com/?attachment_id=xxx and in many cases this will eventually result in a 404 error. To fix the 404, you will want to implement a 301 redirect in your .htaccess but how do you go about doing that?

Our quick snippet here today will handle all of these attachment_id=xx urls and redirect them back to the sites homepage (or any other page you would like). Here it is:

RewriteCond %{QUERY_STRING} ^attachment_id=[0-9]+
RewriteRule ^/?$ http://www.yourdomain.com/? [L,NC,R=301]

You will want to place the above code in your .htaccess file above all of the WordPress rewrite rules. That’s all there is to it, enjoy!


Share Your Thoughts

Leave a Reply

Your email address will not be published. Required fields are marked *


Related Stuff You Might Like







WordPress News, Tips, & Code Snippets

Join the WP Cover mailing list and get wordpress news, tips, code snippets, security warnings, and more delivered right to your inbox.  We won't flood your inbox, newsletters typically go out every 1-2 weeks unless it involves an important security release.

You have Successfully Subscribed!