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

Path rewrite #383

Closed
theduke opened this issue Aug 10, 2016 · 8 comments
Closed

Path rewrite #383

theduke opened this issue Aug 10, 2016 · 8 comments

Comments

@theduke
Copy link

theduke commented Aug 10, 2016

For some pages, I want to to change the URL path in the router to not map directly to the path on the filesystem.

For example: a blog post in a file "blog/2015-01-01-post-title" should have a path "blog/post-title".

Where can I hook into the route building process to change that?

@theduke
Copy link
Author

theduke commented Aug 10, 2016

Ok, seems like Gatsby automatically rewrites the path if the page has a metadata field "path".

This should really be documented somewhere!

@KyleAMathews
Copy link
Contributor

You can also programatically rewrite paths by exporting a rewritePath in gatsby-node.js. Also not documented yet :-\

rewritePath = nodeConfig.rewritePath

See my blog for an example https://github.com/KyleAMathews/blog/blob/master/gatsby-node.js

@janosh
Copy link
Contributor

janosh commented Jun 28, 2018

Would be really cool if some documentation would be added for rewritePath at some point!

Btw, your blog no longer seems to be using rewritePath. Maybe remove the link...

@digitaldiy
Copy link

Can something like next.js approach be implemented?
https://nextjs.org/docs/api-reference/next.config.js/rewrites

@KyleAMathews
Copy link
Contributor

@digitaldiy and others this is a very old issue from pre Gatsby-v1 :-D

Generally speaking there's two options — createRedirect or onCreatePage https://www.gatsbyjs.com/docs/reference/config-files/actions/#createRedirect & https://www.gatsbyjs.com/docs/creating-prefixed-404-pages-for-different-languages/#reach-skip-nav but if you have a specific request/need, please open a new issue!

@digitaldiy
Copy link

Is there a simple way to achieve the code below (next.js), the reason being is that one treatment can have loads of categories but the page is exactly the same so there's no point in duplication but rather a rewrite so people can still see the category/treatment in the search bar but be taken to the right page, this is super useful for categorisation in general ( think how amazon has there departments but products are in loads of categories at the same time but are still listed under the categories you clicked on. Thanks in advance.

async rewrites() {
return [
{
source: '/:category/:treatment',
destination: '/treatments/:treatment',
},
]
},

@KyleAMathews
Copy link
Contributor

Yes, those are redirects. We don't have a way to bulk match the source/destination but you can create redirects for each page while you're creating the pages as well https://www.gatsbyjs.com/docs/reference/config-files/actions/#createRedirect

@digitaldiy
Copy link

Thanks for the link ill go through that now, may I ask if there Is plans to implement a system like this in the feature ( say Gatsby 5), thanks for your help

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

No branches or pull requests

4 participants