Skip to content

Redirect protocol and host#150

Merged
joanniclaborde merged 7 commits intomasterfrom
redirect-protocol-and-host
Nov 13, 2017
Merged

Redirect protocol and host#150
joanniclaborde merged 7 commits intomasterfrom
redirect-protocol-and-host

Conversation

@joanniclaborde
Copy link
Copy Markdown
Contributor

@joanniclaborde joanniclaborde commented Nov 6, 2017

Adds ability to redirect from and to a specific protocol and/or host. It is now possible to use objects for the from and to properties in redirects.js.

If a from object is used (instead of the usual string or regular expression), the following properties are available:

  • protocol - If specified, the request's protocol must match for the whole redirect to match (http or https).
  • host - If specified, the request's host must match for the whole redirect to match (for example site.com).
  • path - The path to match, this is the same as the original from. Defaults to *, to match all paths.

If a to object is used (instead of the usual string or function), the following properties are available:

  • protocol - If specified, will override the request's protocol.
  • host - If specified, will override the request's host.
  • url - The url to redirect to, this is the same as the original to. Defaults to the request's url.

Example to automatically redirect to HTTPS and from the root domain to the www subdomain:

// redirects.js
exports = [
  // http://site.com => https://www.site.com, will preserve the url
  {
    from: {
      protocol: 'http',
      host: 'site.com'
    },
    to: {
      protocol: 'https',
      host: 'www.site.com'
    }
  },
  // http://www.site.com => https://www.site.com, will preserve the url
  {
    from: {
      protocol: 'http',
      host: 'www.site.com'
    },
    to: {
      protocol: 'https'
    }
  }  
];

Copy link
Copy Markdown
Member

@pushred pushred left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Straightforward.

Comment thread package.json Outdated
"continuation-local-storage": "~3.1.1",
"express": "3.1.x",
"express-expose": "SparkartGroupInc/express-expose#escape-script-ending-tag",
"express-expose": "sparkartgroupinc/express-expose#escape-script-ending-tag",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well use the new org name, sparkartgroup

Comment thread lib/server.js
router.engine( DEFAULT_VIEW_EXTENSION, handlebars.engine );
router.set( 'view engine', DEFAULT_VIEW_EXTENSION );
router.set( 'views', paths.views );
router.set( 'trust proxy', true ); // Use the X-Forwarded-* headers: https://expressjs.com/en/guide/behind-proxies.html
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required by Fastly, or any other CDN?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, the HTTPS protocol doesn't seem to be detected by Solidus on Heroku, so HTTPS requests end up in redirect loops.

@joanniclaborde joanniclaborde merged commit ab7b5f7 into master Nov 13, 2017
@joanniclaborde joanniclaborde deleted the redirect-protocol-and-host branch November 13, 2017 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants