Skip to content

Instantly share code, notes, and snippets.

@IlanVivanco
Last active August 1, 2024 23:17
Show Gist options
  • Save IlanVivanco/42502ee0a6c50458d80d40d7e0956550 to your computer and use it in GitHub Desktop.
Save IlanVivanco/42502ee0a6c50458d80d40d7e0956550 to your computer and use it in GitHub Desktop.
Apache config for external assets on local

Setting Up a Proxy Configuration on Apache for LocalWP Sites

This configuration will set up a proxy on Apache to serve files from the production server when they are not available locally in the wp-content/uploads/ directory. This might be useful if you want to perform partial syncs using LocalWP.

Set Up Steps

  1. Open Apache Configuration File

    Open the Apache configuration file for editing. This file is usually located at C:\Users\user\Local Sites\{{your-project}}\conf\apache on Windows.

  2. Add Proxy Configuration

    Add the following configuration to the httpd.conf.hbs file within the <VirtualHost> block.

    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{HTTP_HOST} ^example\.test$
      RewriteRule ^wp-content/uploads/(.*)$ https://example.com/wp-content/uploads/$1 [NC,L]
    </IfModule>
  3. Save and Close the File Save the changes and close the editor.

  4. Reload Apache Reload Apache from the LocalWP dashboard to apply the new configuration.

Explanation

  • <IfModule mod_rewrite.c>: This block ensures that the rewrite rules are only applied if the mod_rewrite module is enabled.
  • RewriteEngine on: Enables the runtime rewriting engine.
  • RewriteBase /: Sets the base URL for per-directory rewrites.
  • RewriteCond %{REQUEST_FILENAME} !-f: This condition checks if the requested filename does not exist as a file.
  • RewriteCond %{HTTP_HOST} ^example\.test$: This condition checks if the HTTP host matches example.test.
  • RewriteRule ... [NC,L]: This rule rewrites requests for files in the wp-content/uploads directory to the production server (https://example.com). The [NC] flag makes the rule case-insensitive, and the [L] flag indicates that this is the last rule to be processed if it matches.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^example\.test$
RewriteRule ^wp-content/uploads/(.*)$ https://example.com/wp-content/uploads/$1 [NC,L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment