Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirect all non trailing slash page requests to the one with a trailing slash #135

Closed
falconmick opened this issue Sep 3, 2018 · 1 comment
Labels

Comments

@falconmick
Copy link
Contributor

falconmick commented Sep 3, 2018

Check out how I did it in Sell My Shares, If I get time I will try to do myself, but all future projects needs this functionality (SEO request)

basically this www.some.com/link needs to be 301'd to www.some.com/link/ just make sure you can still access resources as www.some.com/assets/file.png/ is not going to work

SMS example:
middleware.js

...
  match({routes, location: req.url}, (err, redirect, props) => {
    if (err) {
      res.status(500).json(err);
    } else if (redirect) {
      res.redirect(302, redirect.pathname + redirect.search);
    } else if (props) {
      // !!!!!!!!!! where the magic happens !!!!!!!!!!!
      const hasTrailingSlash = props.location.pathname.match(/(\/\s*$)|(\?.*$)/g);
      if (!hasTrailingSlash) {
        res.redirect(301, props.location.pathname.replace(/\s*$/g, '/'));
      }
      redisClient.get(props.location.pathname, (error, result) => {
        if (result) {
          res.status(200).send(result);
        } else if (props.routes[0].name === 'App') {
          const appFetch = fetchDataForApp(props)
          requestSuccess(appFetch)(props);
        } else {
          requestSuccess()(props);
        }
      });
    } else {
      res.sendStatus(404);
    }
  });
...
@falconmick falconmick added the bug label Sep 3, 2018
@samlogan
Copy link
Contributor

samlogan commented Sep 4, 2018

@falconmick see this PR #137

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants