Scenerio: You’re about to make updates to an existing website – you’ve copied all the files and the database to your local machine – except for the media files (in the case of WordPress, anything under /wp-content/uploads
. Everything is set up and ready to go.
Good thing you didn’t need to wait hours to download all 4gb of that media uploads folder – because you just popped this code snipped into your .htaccess
file (above the WordPress rewrite rules, of course! Nobody ever tells you that part, but I am telling you now. Your welcome.)
This handy snipped will do the following for you:
- Only rewrite URL’s if the hostname matches your dev URL
- Check that the request is asking for stuff within the media uploads folder
- Check your local machine first for existing media (in case you uploaded some as part of your dev changes)
- Then rewrite the URL to the live one where needed
A couple of advantages of doing this are:
- Takes up less disk space locally (not that you were running out, were you?)
- Save yourself a ton of time transferring media files – especially for those video websites or photography websites you are working on
- If any images change on the live server, you see the updates immediately