Roots Discourse

What's the best way to add rewrites?

I’m using the entire Roots stack for the new version of my site. I’m developing using a new fork of my production database. The site is quite large so I wanted to add rewrite rules to swap app/uploads for my production uploads url.

What is the best course of action to do so? I’ve searched quite a bit here and saw some were using htaccess and some are using wp-h5bp-htaccess. I’m having a hard time finding clarity on which method to use.

I gladly donate to roots for help.

If you’re using the entire stack, I don’t think .htaccess will do anything; Trellis is using nginx, not Apache. For things like images, you should be able to do something like this, which basically “falls back” to an external URL if it can’t find things locally:

    location ~* \.(png|jpe?g|gif|bmp|svg|webp|ico)$ {                          
      expires 24h;                                                             
      log_not_found off;                                                       
      try_files $uri $uri/ @production;                                        
    }                                                                          
                                                                        
    location @production {                                                     
      return https://your.production.site/$uri;                                      
    } 

That does assume that you’re referencing assets without the domain and scheme (i.e. you’re doing /asset.jpg not https://site.com/asset.jpg) and that your asset paths aren’t different between production and development (which, if you’re using Roots, they shouldn’t be).

1 Like

My current production site is not part of the Roots stack and uses Apache. I’m building the new version completely separate and currently all development is local.

Am I able to swap upload urls even though the environments are different?

My current production asset urls are: https://domain.com/wp-content/uploads/

Where does that code snippet reside? Thanks for all your patience and help.

You could try putting it in here: https://github.com/roots/trellis/blob/master/roles/wordpress-setup/templates/wordpress-site.conf.j2 Someone else more accustomed to customizing Trellis might have better advice. Play around with it, though!

Am I able to swap upload urls even though the environments are different?

You might be able to. The juice behind that bit of code is try_files, you could read up on it here: http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

1 Like

It took me a minute to figure out how nginx child themes worked but now I have everything sorted. Thank you!

This topic was automatically closed after 42 days. New replies are no longer allowed.