diff --git a/packages/redirect-producer/lib/produce-redirects.js b/packages/redirect-producer/lib/produce-redirects.js index 73d38e0b9b75cc27ce4f00a197097db9c27a0a67..6d51576e597ff4d308634440ca974d9e2bdceda6 100644 --- a/packages/redirect-producer/lib/produce-redirects.js +++ b/packages/redirect-producer/lib/produce-redirects.js @@ -116,11 +116,14 @@ function createNginxRewriteConf (files, urlPath) { fromUrl = ~fromUrl.indexOf('%20') ? `'${urlPath}${fromUrl.replace(ENCODED_SPACE_RX, ' ')}'` : urlPath + fromUrl let toUrl = file.rel.pub.url toUrl = ~toUrl.indexOf('%20') ? `'${urlPath}${toUrl.replace(ENCODED_SPACE_RX, ' ')}'` : urlPath + toUrl + const toHostPrefix = '$scheme://$http_host' if (file.pub.splat) { const toUrlWithTrailingSlash = ensureTrailingSlash(toUrl) - return `location ^~ ${fromUrl}/ { rewrite ^${regexpEscape(fromUrl)}/(.*)$ ${toUrlWithTrailingSlash}$1 redirect; }` + return `location ^~ ${fromUrl}/ { rewrite ^${regexpEscape( + fromUrl + )}/(.*)$ ${toHostPrefix}${toUrlWithTrailingSlash}$1 redirect; }` } else { - return `location = ${fromUrl} { return 301 ${toUrl}; }` + return `location = ${fromUrl} { return 301 ${toHostPrefix}${toUrl}; }` } }) return [new File({ contents: Buffer.from(rules.join('\n') + '\n'), out: { path: '.etc/nginx/rewrite.conf' } })] diff --git a/packages/redirect-producer/test/produce-redirects-test.js b/packages/redirect-producer/test/produce-redirects-test.js index 02daca164acaacdf717aaecec3d41966f9881598..6e29898a7cec15a13d30669a244b05d38bfc0488 100644 --- a/packages/redirect-producer/test/produce-redirects-test.js +++ b/packages/redirect-producer/test/produce-redirects-test.js @@ -509,11 +509,11 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - 'location = /component-a/module-a/alias-a.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /component-a/module-a/old-target/index.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /component-a/module-b/alias-b.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /component-b/1.0/alias-c.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /index.html { return 301 /component-a/module-a/the-target.html; }', + 'location = /component-a/module-a/alias-a.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /component-a/module-a/old-target/index.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /component-a/module-b/alias-b.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /component-b/1.0/alias-c.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /index.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', ]) }) @@ -537,7 +537,7 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - 'location ^~ /component-a/current/ { rewrite ^/component-a/current/(.*)$ /component-a/1.0/$1 redirect; }', + 'location ^~ /component-a/current/ { rewrite ^/component-a/current/(.*)$ $scheme://$http_host/component-a/1.0/$1 redirect; }', ]) }) @@ -555,7 +555,7 @@ describe('produceRedirects()', () => { const result = produceRedirects(playbook, contentCatalog) expect(result).to.have.lengthOf(1) const rules = extractRules(result[0]) - expect(rules).to.eql(['location ^~ /3.0/ { rewrite ^/3\\.0/(.*)$ /$1 redirect; }']) + expect(rules).to.eql(['location ^~ /3.0/ { rewrite ^/3\\.0/(.*)$ $scheme://$http_host/$1 redirect; }']) }) it('should not add extra trailing slash to splat alias when site path is non-empty', () => { @@ -573,7 +573,9 @@ describe('produceRedirects()', () => { const result = produceRedirects(playbook, contentCatalog) expect(result).to.have.lengthOf(1) const rules = extractRules(result[0]) - expect(rules).to.eql(['location ^~ /docs/3.0/ { rewrite ^/docs/3\\.0/(.*)$ /docs/$1 redirect; }']) + expect(rules).to.eql([ + 'location ^~ /docs/3.0/ { rewrite ^/docs/3\\.0/(.*)$ $scheme://$http_host/docs/$1 redirect; }', + ]) }) it('should escape special regex characters in splat pattern', () => { @@ -596,7 +598,7 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - 'location ^~ /component-c++/1.0/ { rewrite ^/component-c\\+\\+/1\\.0/(.*)$ /component-c++/latest/$1 redirect; }', + 'location ^~ /component-c++/1.0/ { rewrite ^/component-c\\+\\+/1\\.0/(.*)$ $scheme://$http_host/component-c++/latest/$1 redirect; }', ]) }) @@ -614,7 +616,7 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - "location = '/component-a/module-a/alias with spaces.html' { return 301 '/component-a/module-a/target with spaces.html'; }", + "location = '/component-a/module-a/alias with spaces.html' { return 301 '$scheme://$http_host/component-a/module-a/target with spaces.html'; }", ]) }) @@ -626,11 +628,11 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - 'location = /docs/component-a/module-a/alias-a.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/component-a/module-a/old-target/index.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/component-a/module-b/alias-b.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/component-b/1.0/alias-c.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/index.html { return 301 /docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-a/module-a/alias-a.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-a/module-a/old-target/index.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-a/module-b/alias-b.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-b/1.0/alias-c.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/index.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', ]) }) @@ -642,11 +644,11 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - 'location = /docs/component-a/module-a/alias-a.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/component-a/module-a/old-target/index.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/component-a/module-b/alias-b.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/component-b/1.0/alias-c.html { return 301 /docs/component-a/module-a/the-target.html; }', - 'location = /docs/index.html { return 301 /docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-a/module-a/alias-a.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-a/module-a/old-target/index.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-a/module-b/alias-b.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/component-b/1.0/alias-c.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', + 'location = /docs/index.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }', ]) }) @@ -658,7 +660,7 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.include( - 'location = /docs/component-a/module-a/alias-a.html { return 301 /docs/component-a/module-a/the-target.html; }' + 'location = /docs/component-a/module-a/alias-a.html { return 301 $scheme://$http_host/docs/component-a/module-a/the-target.html; }' ) }) @@ -670,11 +672,11 @@ describe('produceRedirects()', () => { expect(result[0].contents.toString()).to.endWith('\n') const rules = extractRules(result[0]) expect(rules).to.eql([ - 'location = /component-a/module-a/alias-a.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /component-a/module-a/old-target/index.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /component-a/module-b/alias-b.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /component-b/1.0/alias-c.html { return 301 /component-a/module-a/the-target.html; }', - 'location = /index.html { return 301 /component-a/module-a/the-target.html; }', + 'location = /component-a/module-a/alias-a.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /component-a/module-a/old-target/index.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /component-a/module-b/alias-b.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /component-b/1.0/alias-c.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', + 'location = /index.html { return 301 $scheme://$http_host/component-a/module-a/the-target.html; }', ]) })