The Perfect WebRedirects module will filter for specified routes (including trailing wildcard routes) and perform redirects as instructed if a match is found.
A demo showing the usage, and working of the Perfect WebRedirects module can be found at https://github.com/PerfectExamples/Perfect-WebRedirects-Demo.
Import the dependency into your project by specifying it in your project's Package.swift file, or adding it via Perfect Assistant.
.Package(url: "https://github.com/PerfectlySoft/Perfect-WebRedirects", majorVersion: 3),
Then in your main.swift
file where you configure your web server, add it as an import, and add the filter:
import PerfectWebRedirects
Adding the filter:
// Add to the "filters" section of the config:
[
"type":"request",
"priority":"high",
"name":WebRedirectsFilter.filterAPIRequest,
]
If you are also adding Request Logger filters, if the Web Redirects object is added second, directly after the RequestLogger filter, then both the original request (and it's associated redirect code) and the new request, will be logged correctly.
The configuration for the routes is included in JSON files at /config/redirect-rules/*.json
in the form:
{
"/test/no": {
"code": 302,
"destination": "/test/yes"
},
"/test/no301": {
"code": 301,
"destination": "/test/yes"
},
"/test/wild/*": {
"code": 302,
"destination": "/test/wildyes"
},
"/test/wilder/*": {
"code": 302,
"destination": "/test/wilding/*"
}
}
Note that multiple JSON files can exist in this directory - all will be loaded the first time the filter is invoked.
The master branch of this project currently compiles with Xcode 9 or the Swift 4 toolchain on Ubuntu.
For more information on the Perfect project, please visit perfect.org.