Setup nginx-include for custom redirect

Hi,

I have already followed the Custom Nginx Includes in Trellis | Trellis Docs | Roots docs. But maybe I am getting something wrong?

I am trying to setup the following redirects:
https://www.mydomain.co.uk/wp/app/uploads/sites/9/
to
https://www.mydomain.co.uk/app/uploads/sites/11/

wordpress_sites.yml

wordpress_sites:
  nonecom.comfygroup.com:
    site_hosts:
      - canonical: www.mydomain.co.uk
        redirects:
          - mydomain.co.uk
    local_path: ../non-ecom-site

I have therefore setup the following file/folder structure:

/trellis/nginx-includes/www.mydomain.co.uk/media-url-redirects.conf.j2

And the contents of that conf file is:

{{ ansible_managed }}

Redirect old Gravity Forms uploads from /sites/9/ to /sites/11/

location ~ ^/app/uploads/sites/9/(.*)$ {
  return 301 /app/uploads/sites/11/$1;
}

Also catch any accidental /wp/app/uploads/ prefix

location ~ ^/wp/app/uploads/sites/9/(.*)$ {
  return 301 /app/uploads/sites/11/$1;
}

So far my nginx config/redirects are not being applied to the server at all. Is there any obvious setup issue so far? Thanks.

Notes, my wordpress_sites file has many more domains, but I have removed for security. This redirect should apply for the mydomain.co.uk domain only.

Untested — make sure your folder name in nginx-includes/ matches your wordpress_sites name, not the site host:

rewrite ^/app/uploads/sites/9/(.*)$ /app/uploads/sites/11/$1 permanent;
rewrite ^/wp/app/uploads/sites/9/(.*)$ /app/uploads/sites/11/$1 permanent;

Here’s a real rewrites example from the roots.io site:

# @ trellis/nginx-includes/roots.io/rewrites.conf.j2
# (roots.io is the wordpress_sites name)

rewrite ^/plugins/bcrypt-password/$ https://github.com/roots/wp-password-bcrypt permanent;

Thanks Ben. Working, I was using a site_host url instead. Plus I kept provisioning with –nginx tag only but I also needed to do a full provision before even your changes would show up too.

1 Like