Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you have a conditional in the htaccess file

Been looking around the web to add a production rule in the .htaccess file. I have a wordpress website; one for production and the other, staging.

When a file is uploaded, it goes to AWS (s3). I need to prevent this behaviour for staging.

The code that sets the asset path is, in the .htaccess file:

RewriteRule ^wp-content/uploads/(.*)$ https://s3-eu-west-1.amazonaws.com/<BUCKET-NAME>/wp-content/uploads/$1 [R=301,L]

I cant seem to find an "if statement" or some sort of condition to use. Honestly, I think this is not possible. Is it?

I only need to run that code for production and not staging. Staging url is different from production.

Updated

Whenever I use below, my website crashes:

<If "-z req('Host') == 'www.<PRODUCTION>.com/'">
  RewriteRule ^wp-content/uploads/(.*)$ https://s3-eu-west-1.amazonaws.com/<BUCKET-NAME>/wp-content/uploads/$1 [R=301,L]
</If>
like image 441
Sylar Avatar asked Sep 15 '25 10:09

Sylar


1 Answers

If directive works on Apache 2.4 and newer versions. On lower versions you can use RewriteCond directive to conditionally rewrite urls.

You can use something like this

RewriteEngine on
#if host == "www.production.com"
RewriteCond %{HTTP_HOST} ^www.production.com$ [NC]
 # execute the rule
 RewriteRule ^wp-content/uploads/(.*)$ https: //s3-eu-west-1.amazonaws.com/<BUCKET-NAME>/wp-content/uploads/$1 [R=301,L]
like image 80
Amit Verma Avatar answered Sep 18 '25 10:09

Amit Verma