Rewrite Specific WordPress Uploads Folders with .htaccess

I need to redirect some WordPress uploads folders and leave others alone. For example, I need to redirect http://www.mysite.com/wp-content/uploads/2009/08/image.jpg to http://www.mysite.com/assets/2009/08/image.jpg.

I tried this:

Read More
RedirectMatch 301 http://www.mysite.com/assets/2009/08/(.*)$ http://www.mysite.com/wp-content/uploads/2009/08/$1

I am using RedirectMatch for other redirects and they are working so RedirectMatch is enabled.

I am not sure what I am missing.

Thanks for any help.

Related posts

Leave a Reply

1 comment

  1. Protocol and hostnames don’t go into the match in the RedirectMatch directive. It only matches against the URI (everything after the hostname, e.g. /wp-content/uploads/2009/08/image.jpg). Also, since you are redirecting to the same site, you don’t need the http://www.mysite.com in your target either:

    RedirectMatch 301 ^/assets/2009/08/(.*)$ /wp-content/uploads/2009/08/$1
    

    But the question you’re asking is:

    I need to redirect http://www.mysite.com/wp-content/uploads/2009/08/image.jpg to http://www.mysite.com/assets/2009/08/image.jpg.

    Which would mean you’ve got the 2 things backwards if you want to go from wp-content to assets. You’d need to swap them:

    RedirectMatch 301 ^/wp-content/uploads/2009/08/(.*)$ /assets/2009/08/$1
    

    Or you can be even more general:

    RedirectMatch 301 ^/wp-content/uploads/([0-9]{4})/([0-9]{2})/(.*)$ /assets/$1/$2/$3