WordPress Re-directing ?attachment_id=xx To An Alternative URL
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