Skip to content

Instantly share code, notes, and snippets.

@rxnlabs
Last active November 18, 2022 15:06
Show Gist options
  • Save rxnlabs/b5cfa10fcb599c8ff829 to your computer and use it in GitHub Desktop.
Save rxnlabs/b5cfa10fcb599c8ff829 to your computer and use it in GitHub Desktop.
htaccess - Redirect image and file request from the localhost to the production host if the file does not exist on the localhost. Needs to be placed in the root htaccess if you're using WordPress or another system that redirects all request to a index.php file.
#place above all other rewrite rules if using a cms or program that redirects all request to index.php file (e.g. WordPress, Drupal, Laravel, etc...)
#if a file or directory does not exist on the local host, 302 redirect the request to the production host so the browser doesn't cache the file in case you replace the file locally
<IfModule mod_rewrite.c>
RewriteEngine On
#Redirect WordPress uploads
#Prevent infinite redirect. Only apply this if not on any subdomains of the live domain. Comment out line if not needed
RewriteCond %{HTTP_HOST} !^(.*)example\.com
#Prevent infinite redirect. Only apply this if not on the root domain of the live domain. Uncomment line if needed
#RewriteCond %{HTTP_HOST} !^example\.com [NC]
#Prevent infinite redirect. Only apply this if not on the www version of the exact domain of the live domain. Uncomment line if needed
#RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule wp-content/uploads/(.*)$ https://www.example.com/wp-content/uploads/$1 [R=302,L,NC]
#Redirect Nextgen Gallery uploads
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule wp-content/gallery/(.*)$ https://www.example.com/wp-content/gallery/$1 [R=302,L,NC]
</IfModule>
@NickDeckerDevs
Copy link

Beautiful person you!

@goodmuyis
Copy link

Nice I lof you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment