Apache rewrite using THE_REQUEST for rewriting GET requests only

6,709

Looks like I was really close, and had the condition and rule in the wrong place (it was at the bottom, but should have been at the top).

This is what's working:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^GET [NC]
    RewriteRule ^media/(.*) http://s3-us-west-2.amazonaws.com/bucketname/media/$1 [QSA,NC,NE,R,L]
    RewriteRule ^index\.php$ - [L]
    RewriteRule ^assets/(.*) /wp-content/themes/themename/assets/$1 [QSA,L]
    RewriteRule ^plugins/(.*) /wp-content/plugins/$1 [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>
Share:
6,709

Related videos on Youtube

Nick Abbey
Author by

Nick Abbey

DevOps for Evolving Media Networks

Updated on September 18, 2022

Comments

  • Nick Abbey
    Nick Abbey almost 2 years

    Background:

    In order to reduce repo size of git repos containing wordpress installs, we are removing static assets from the repo. The bulk of the work is done, but I have a snag. I've relocated the assets to an S3 bucket and used an .htaccess RedirectPermanent so that requests for local media are redirected to the s3 bucket. This is great to manage all the existing assets that are in a new place without modifying the wp database. However, for newly added assets, we're using the 'Amazon S3 and Cloudfront' plugin to automatically move them to the same S3 bucket after they are uploaded to the server. However, for it to work I need to change my RedirectPermanent to RewriteRule using THE_REQUEST so that only the GET requests for media are pointed to the S3 bucket. This is because the 'Amazon S3 and Cloudfront' plugin needs to allow the file to be uploaded locally as a cache, from which it pushes to the S3 bucket.

    Here's what we have in the .htaccess currently:

    RedirectPermanent /media https://s3-us-west-2.amazonaws.com/bucket/media
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteRule ^assets/(.*) /wp-content/themes/theme/assets/$1 [QSA,L]
    RewriteRule ^plugins/(.*) /wp-content/plugins/$1 [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    

    I'm not great with Apache Rewrite Rules and conditions, I'm specifically weak in the area of formatting the regexes for the match, and my level of expertise with actually writing the rewrite rule could be better as well. I've read through a few online examples of how to use THE_REQUEST and I know that I need something like:

    #there's definitely more that I need here, but I don't know what I'm missing
    RewriteCond %{THE_REQUEST} ^GET /media/(*)  
    #It kind of goes off the rails here, I'm very unsure of this syntax
    RewriteRule ^media/(*) https://s3-us-west-2.amazonaws.com/fklassets/media/$1 [L]
    

    Additionally, I'm not sure where in the .htaccess file the new condition and rule belong.