Redirect anything within /attachments/ directory up one level but also removing anything after /attachments/

Posted by Rich Staats on Server Fault See other posts from Server Fault or by Rich Staats
Published on 2012-04-06T22:53:07Z Indexed on 2012/04/06 23:32 UTC
Read the original article Hit count: 316

Filed under:
|

WordPress creates an attachment page for any images uploaded through the post editor and "attached" to that post. After migrating a site, those attachment pages no longer exist, and we now have around 1000 links pointed at 404's.

So, I was looking to find a way to do a redirect for any url that has /attachement/ in it's string and then push that url back up one level (which happens to be the post page). so for instance:

http://mysite.com/2012/news/blog-post-title/attachment/image-page/ (which doesnt exist) will go to http://mysite.com/2012/news/blog-post-title/ (which does exist).

Along with redirecting up one level, I also need to remove anything after /attachment/ (in this case the "image-page."

Any suggestions?

Thanks in advance

© Server Fault or respective owner

Related posts about .htaccess

Related posts about redirects