完美的 Web 重定向

Get Involed with Perfect!

Star Perfect On Github Stack Overflow Follow Perfect on Twitter Join the Perfect Slack

Swift 4.0 Platforms OS X | Linux License Apache PerfectlySoft Twitter Slack Status

完美的 Web 重定向演示

Perfect WebRedirects 模块将过滤指定的路由(包括尾部通配符路由),如果找到匹配项,则按照指示执行重定向。

可以在 https://github.com/PerfectExamples/Perfect-WebRedirects-Demo 找到展示 Perfect WebRedirects 模块的用法和工作原理的演示。

包含在你的项目中

通过在项目的 Package.swift 文件中指定依赖项,或者通过 Perfect Assistant 添加,将依赖项导入到你的项目中。

.Package(url: "https://github.com/PerfectlySoft/Perfect-WebRedirects", majorVersion: 3),

然后在你配置 Web 服务器的 main.swift 文件中,将其添加为导入,并添加过滤器

import PerfectWebRedirects

添加过滤器

// Add to the "filters" section of the config:
[
	"type":"request",
	"priority":"high",
	"name":WebRedirectsFilter.filterAPIRequest,
]

如果你也添加了 Request Logger 过滤器,如果 Web Redirects 对象在 RequestLogger 过滤器之后立即添加,那么原始请求(及其关联的重定向代码)和新请求都将被正确记录。

配置文件

路由的配置包含在 /config/redirect-rules/*.json 的 JSON 文件中,格式如下

{

  "/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/*"
  }

}

请注意,此目录中可以存在多个 JSON 文件 - 第一次调用过滤器时,所有文件都将被加载。

与 Swift 的兼容性

该项目的主分支目前在 Ubuntu 上使用 Xcode 9Swift 4 工具链进行编译。

更多信息

有关 Perfect 项目的更多信息,请访问 perfect.org