Unlike domain redirects, path redirects allow you to redirect from a specific URL path on your domain. The target is usually a different path on the same domain, although it is possible to redirect to a different URL altogether. This functionality makes use of the Nginx rewrite directive.
Creating a Path Redirect
To view and create path redirects in SpinupWP navigate to the Path Redirects tab for a site. Click “Add Path Redirect” to create a new redirect.
There are three parts required to set up a path redirect:
- a matching string
- a replacing string
- a redirect type
The matching string tells the redirect what paths should be redirected. If the path requested by the client (a browser, or a search engine robot) matches the matching string, the redirect is enabled. The matching string can be a plain text string or a regex string.
The replacing string is the path (or domain) that the request should be redirected to. The replacing string can also be a plain text string or a regex string.
The redirect type gives you the option to set the redirect to be permanent, or temporary should you want to remove or update it at a later stage. This is the HTTP code that the Nginx server returns to the client when the redirect takes place, and is used by browsers and search engines.
Path Redirect Examples
A plain text string match allows you to perform what is known as a “loose match” based on a specific path on your site. Let’s look at a simple example of how you would use this.
Let’s say you have a Work page which can be accessed at
https://hellfish.media/work/. You decide to change the title of that page to “Our Work” and update the page slug to
our-work. This will mean that the new page is accessed at
https://hellfish.media/our-work/. But you want to redirect any requests from the old URL to the new one.
If you set the matching string to
/work and the replacing string to
/our-work/, all requests to the old path will be redirected to the new one.
The above match is a loose match, which means any time the URL path contains
/work, it will trigger a match. So the URL
https://hellfish.media/posts/work-on-your-own-time/ would also match, which we don’t want. In this case you can specify the following match
^/work. Adding the
^ character means only matching with the string “/work” at the beginning of the URL path.
However, if you had a custom permalink for the Careers page URL at
https://hellfish.media/work/careers/, it would also be redirected to
https://hellfish.media/our-work/, which is not ideal. In this case, you could use a regular expression (regex) match for a more specific redirect.
In the Careers example above, let’s say you only wanted to redirect
https://hellfish.media/our-work/, but you wanted to leave
https://hellfish.media/work/careers/ as is. Using regex, if you set your matching string to
^/work/?$ this will trigger the redirect only if the path exactly matches
/work and redirect it. In this case,
https://hellfish.media/work/ would redirect to
https://hellfish.media/work/careers/ would not.
Taking this a step further, if you also decided to update the Careers page custom permalink, and you wanted to redirect
https://hellfish.media/our-work/careers/, you could update the matching string to
^/work/(.*) and the redirecting string to
/our-work/$1. This will perform a redirect but also maintain any URL paths that exist after the path match:
https://hellfish.media/work/would redirect to
https://hellfish.media/work/careers/would redirect to
Finally, with any of the above matches, you could also specify a different domain in the replacing string, and the browser will be redirected to that domain.
Another typical example of this is where your WordPress permalinks are set up to include the category slug in the URL, and you change category slugs.
Setting the matching string to
^/old-category-slug/(.*) and the redirecting string to
/new-category-slug/$1 will redirect any requests to posts on the old category slug to the new one, but will preserve the post name in the URL.