URL Remapping in Trellis?

Alright, Roots Community,

You all were instrumental in helping me understand capabilities with Trellis in the past, and as a junior developer, I can use all of the help I can get. I believe this is an NGINX related question however I would love any other solutions that help achieve the same mission of redirecting URLs to different paths. I am looking to be able to do two things on a Roots installation to help clean up some items on a WordPress site I am playing with:

  1. I have a present multisite set-up with a domain of foo.maindomain.net, and I would like to see if I can remap the domain and subsequent URL’s to maindomain.net/foo. I believe this is as simple as an upstream and proxy pass, however, I am not getting any actionable results. I have placed my NGINX includes files in the right directory as outlined in the Trellis Documentation here, however I am getting quite a few “directive is not allowed here” errors. I am still very new to NGINX and it’s capabilities, so I may be using NGINX incorrectly, or perhaps shouldn’t be using it at all. The code I attempted to use in my local dev environment in the proxy.conf.j2 file can be found below…

    upstream build_change_annual_reports {

    server {
    listen 80;
    location /annualreports {
    #trailing slash is vital to operation
    proxy_pass https://annualreports.buildchange.test/;

  2. Lastly, I have another redirection related question: I am working with an existing Wordpress site that unfortunately has a quite few files in a directory outside of Wordpress and it’s core file structure. For security, I have brought those files back into the roots directory, but now need to find a solution for users who have existing links to these files. I preferably don’t want to run a 301 request for each of these files as there are some 300 or so files that were once in the external directory. The goal at the end of the day is to take any file that was once in the /pdfs/ directory and convert it to the Roots-based /app/uploads/ directory instead.

Any help would be greatly appreciated, and I acknowledge these questions may not be directly related to Roots, however, given my little knowledge of NGINX, I have normally been able to scrape by. Given that Roots has now entered my stack, I haven’t figured out how to work through these two issues just yet. If you have solutions for either of these issues not related to NGINX, feel free to pitch them too.

Thank you for your help in advance!

No idea on the multisite stuff, but your second question seems to be a pretty straightforward application of the rewrite directive: https://www.nginx.com/blog/creating-nginx-rewrite-rules/

@alwaysblank, That is what I thought too for problem #2. I have added the rewrite rules using the NGINX Includes documentation as stated above, and my two rules I need to set are below:
rewrite ^/wp-content/(.)$ /app/$1 last;
rewrite ^/pdfs/(.
)$ /app/uploads/$1 last;

However, after a local re-provision and a quick test, the new rules do not seem to be taking place. Now if I am reading the NGINX documentation you posted correctly, it looks like these rewrite commands need to be at the server{} level rather than the http{} level, do you have any idea how I might be able to achieve such an elevation safely within Roots?

Like the return directive, you enclose the rewrite directive in a server or location context that defines the URLs to be rewritten.

I’m not sure what you mean by “http{} level,” but yes, the NGINX documentation says that rewrite needs to be in a server or location context.

The docs you linked have an example of this: https://roots.io/trellis/docs/nginx-includes/#child-template-example-complex

You can also look through Trellis’s NGINX conf files to see other examples.

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