From 3218d4ca5b82a90965d98294f2b1e2375f0df2ea Mon Sep 17 00:00:00 2001 From: n p Date: Thu, 28 Apr 2022 18:05:10 +0200 Subject: [PATCH 1/6] Passes cloudfront_function_301_redirects to cloudfront module --- main.tf | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/main.tf b/main.tf index 68f9d39..4b58215 100644 --- a/main.tf +++ b/main.tf @@ -29,8 +29,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" { From 58f9eaca4be8e6f9df229dbeb03e0ec0c9ceced6 Mon Sep 17 00:00:00 2001 From: n p Date: Fri, 29 Apr 2022 10:29:19 +0200 Subject: [PATCH 2/6] Fixes missing uri param --- modules/cloudfront/function_rewrite/index.js.tftpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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', From f4f92a8e799a604c174dcc5b2765c3f53dcab5cc Mon Sep 17 00:00:00 2001 From: n p Date: Fri, 29 Apr 2022 10:49:41 +0200 Subject: [PATCH 3/6] Passes plugin version from top level module --- main.tf | 2 ++ variables.tf | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/main.tf b/main.tf index 4b58215..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" { diff --git a/variables.tf b/variables.tf index 076cc26..bde1c8a 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/leonstafford/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." From 4584fc12cbd77f5d3808209ec3419ba35c920792 Mon Sep 17 00:00:00 2001 From: n p Date: Fri, 29 Apr 2022 10:50:55 +0200 Subject: [PATCH 4/6] Updates WP2Static url as repo was migrated --- modules/codebuild/variables.tf | 2 +- variables.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 bde1c8a..b9f204e 100644 --- a/variables.tf +++ b/variables.tf @@ -156,7 +156,7 @@ variable "wordpress_memory_limit" { 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" } From 5ce8093e14809a104e980f6cdf79c9c5889ef029 Mon Sep 17 00:00:00 2001 From: n p Date: Fri, 29 Apr 2022 12:56:21 +0200 Subject: [PATCH 5/6] Adds some help of permanent redirects --- .header.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/.header.md b/.header.md index 59be3d0..1e3d812 100644 --- a/.header.md +++ b/.header.md @@ -205,10 +205,34 @@ You may now edit Wordpress as you would normally, customize your site as you lik section of the WP2Static plugin, and click the 'Generate Static Site' button. This will take some minutes depending on the size of your site. When complete the site will be published in S3, and available via the public URL configured in your module definition. - + cloudfront_function_301_redirects = { + # Old category pages + "^\\/(pre-historia)$": "/categoria/$1/", 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 From 4064c66ff3fe79ff9008749813ce66e7c1ecbde0 Mon Sep 17 00:00:00 2001 From: n p Date: Fri, 29 Apr 2022 12:57:41 +0200 Subject: [PATCH 6/6] Removed bad paste --- .header.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.header.md b/.header.md index 1e3d812..a8de632 100644 --- a/.header.md +++ b/.header.md @@ -205,9 +205,7 @@ You may now edit Wordpress as you would normally, customize your site as you lik section of the WP2Static plugin, and click the 'Generate Static Site' button. This will take some minutes depending on the size of your site. When complete the site will be published in S3, and available via the public URL configured in your module definition. - cloudfront_function_301_redirects = { - # Old category pages - "^\\/(pre-historia)$": "/categoria/$1/", + 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.