diff --git a/.header.md b/.header.md index 59be3d0..a8de632 100644 --- a/.header.md +++ b/.header.md @@ -209,6 +209,28 @@ in your module definition. Gentle reminder that no backup options are currently bundled with this module - the most effective means would be to generate and retain a backup from within Wordpress for maximum flexibility. We recommend the UpdraftPlus plugin. +## Permanent Redirects + +Basic url path based permanent redirects are supported via the CloudFront function. The variable `cloudfront_function_301_redirects` can be set with a custom map of match to destination mappings. + +Some aspects that need to be taken into consideration for the match: + +* It's a regular expression +* Group replacements are supported +* Runs in a Javascript function, escaping needs to be taken into consideration +* Passed through a TF var, so escaping that needs to be taking into account as well + +An example to match a path like `/category-name`, a suitable match would be `"^\\/(category-name)$"`. Breaking down the `\\/` part, the first `\` tells TF to escape the second `\`, which is the Regex escape for the `/` character. + +An example: + +``` +cloudfront_function_301_redirects = { + # Redirects /travel to /category/travel/ + "^\\/(travel)$": "/category/$1/", +} +``` + ## Troubleshooting If you experience issues with the publish element of WP2Static, you can retry. It can be more reliable to proceed to diff --git a/main.tf b/main.tf index 68f9d39..be2e09b 100644 --- a/main.tf +++ b/main.tf @@ -16,6 +16,8 @@ module "codebuild" { wordpress_ecr_repository = aws_ecr_repository.serverless_wordpress.name aws_account_id = var.aws_account_id container_memory = var.ecs_memory + wp2static_version = var.wp2static_version + wp2static_s3_addon_version = var.wp2static_s3_addon_version } module "cloudfront" { @@ -29,8 +31,10 @@ module "cloudfront" { } depends_on = [aws_acm_certificate_validation.wordpress_site, module.waf] - cloudfront_class = var.cloudfront_class - waf_acl_arn = var.waf_enabled ? module.waf[0].waf_acl_arn : null + + cloudfront_class = var.cloudfront_class + waf_acl_arn = var.waf_enabled ? module.waf[0].waf_acl_arn : null + cloudfront_function_301_redirects = var.cloudfront_function_301_redirects } module "waf" { diff --git a/modules/cloudfront/function_rewrite/index.js.tftpl b/modules/cloudfront/function_rewrite/index.js.tftpl index 381de41..a379415 100644 --- a/modules/cloudfront/function_rewrite/index.js.tftpl +++ b/modules/cloudfront/function_rewrite/index.js.tftpl @@ -5,7 +5,7 @@ function handler(event) { try { %{ for match, target in REDIRECTS } if (/${match}/.test(uri)) { - return permanentRedirect(/${match}/, '${target}'); + return permanentRedirect(uri, /${match}/, '${target}'); } %{ endfor ~} @@ -23,7 +23,7 @@ function handler(event) { return request; } -function permanentRedirect(match, target) { +function permanentRedirect(uri, match, target) { return { statusCode: 301, statusDescription: 'Moved Permanently', diff --git a/modules/codebuild/variables.tf b/modules/codebuild/variables.tf index 4c338b3..96273ad 100644 --- a/modules/codebuild/variables.tf +++ b/modules/codebuild/variables.tf @@ -66,7 +66,7 @@ variable "graviton_codebuild_enabled" { variable "wp2static_version" { type = string - description = "Version of WP2Static to use from https://github.com/leonstafford/wp2static/releases" + description = "Version of WP2Static to use from https://github.com/WP2Static/wp2static/releases" default = "7.1.7" } diff --git a/variables.tf b/variables.tf index 076cc26..b9f204e 100644 --- a/variables.tf +++ b/variables.tf @@ -154,6 +154,18 @@ variable "wordpress_memory_limit" { default = "256M" } +variable "wp2static_version" { + type = string + description = "Version of WP2Static to use from https://github.com/WP2Static/wp2static/releases" + default = "7.1.7" +} + +variable "wp2static_s3_addon_version" { + type = string + description = "Version of the WP2Static S3 Add-on to use from https://github.com/leonstafford/wp2static-addon-s3/releases/" + default = "1.0" +} + variable "waf_acl_rules" { type = list(any) description = "List of WAF rules to apply. Can be customized to apply others created outside of module."