Rewrite URL

What are methods of doing URL rewrites? I am using trellis, bedrock and sage.
I have having trouble trying to rewrite the url from:



I tried to put rewrite directives inside vagrant machine first: /etc/nginx/wordpress.conf
Before I actually add it to the provisioning template file: trellis\roles\wordpress-setup\templates\wordpress-site.conf.j2

I am new to nginx and having trouble writing the correct rewrite rule.
Any help is appreciated!

Thanks in advanced.

This is a good read if your starting out with trying to lock down nginx rewrites vs. return and what is best practice as well as examples.

I just skimmed through it and one of the examples looks like it directly relates to what your asking.

I understand that this is filed under trellis, and I apologize if this is totally off base for your specific use case.

Curious if you’ve considered using some of the WP core rewrite functions like add_permastruct and add_rewrite_rule instead of making edits to your nginx confs? e.g

function app_rewrite_basic() {
  add_rewrite_rule('^services/([^/]*)/?', 'services.php?page=$matches[1]', 'top');
  add_rewrite_rule('^services/([^/]*)/([^/]*)/?', 'services.php?page=$matches[1]&pn=$matches[2]', 'top');
add_action('init', 'app_rewrite_basic');

Anywho, I did find this article to be rather helpful in guiding me around nginx rewrites.

Thanks @GFargo

It actually doesn’t matter to me which method of rewrite I have to use as long as it gets the job done.
I’ll try the wordpress way.


I just tried it and found out WordPress Rewrite API does not work in an nginx server.

Hey @wchen02,

I’m currently run WordPress on nginx without any issue; have you checked out this codex page on Nginx in WordPress?

It sounds like you may need to adjust your nginx settings. Is this a multisite or single install we are talking about?

Per Multisite Docs:

To make WordPress work with Nginx you have to configure the backend php-cgi.

If you are already working with WordPress on your server it’s probably worth figuring out why the their rewrite API isn’t functioning. Here is a post that breaks down the rules you’ll need to add to your config block.

This is a Single install.
The site is working fine, but not the URL rewrites (or that I don’t know how).

Thanks for the post. I’ll try it out.

I’m not sure how you came to the conclusion that rewrites don’t work with NGINX. If you see and not or something, then rewrites are working.

1 Like


If you don’t already I’d recommend getting WP Debug Bar plugin. It supports multiple add-on plugins one of which includes Debug Bar Rewrite Rules.

It could be that the desired permastruct that you’ve added is being ignored for a higher priority match. The plugin above should assist in figuring this out.

Thanks @kalenjohnson and @GFargo

I got the url rewrite to work using WordPress Rewrite API.

Here is what I did to rewrite the following 2 URLs:

  • /service/app -> /service?page=app
  • /designs/desktop/1 -> /designs?page=desktop&pn=1


$sage_includes = [
‘lib/assets.php’, // Scripts and stylesheets
’lib/extras.php’, // Custom functions
’lib/setup.php’, // Theme setup
’lib/titles.php’, // Page titles
’lib/wrapper.php’, // Theme wrapper class
’lib/customizer.php’, // Theme customizer
’lib/wp_bootstrap_navwalker.php’, // Bootstrap nav walker
‘lib/url_rewrite.php’ // add this


<?php namespace Sage\Rewrites;

function listen_rewrite_action() {
add_action( ‘init’, __NAMESPACE__ . ‘\listen_rewrite_action’ );

Go to admin backend settings -> permalinks -> save changes