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

Change Page's Publish Directory #29

Closed
expatjedi opened this issue Apr 17, 2021 · 4 comments
Closed

Change Page's Publish Directory #29

expatjedi opened this issue Apr 17, 2021 · 4 comments

Comments

@expatjedi
Copy link

Hello. I have built my blog and it works great. There is one thing i want to do but i don't know how. My english is limited to search the issue. I could make this in hugo with a single permalinks parameter.

My blog posts publishing under /blog directory and when i called a page, it shows like https://url/blog/sample-page so i would like to change this like https://url/sample-page.

How do i do this? Any help would be appreciated.

@timlrx
Copy link
Owner

timlrx commented Apr 17, 2021

Hi, it's slightly different with nextjs since it uses file-based routing - the posts in /pages/blog/[slug] are routed to https://url/blog/slug. If you want to change it posts instead of blog, that would be relatively simple since you can move [slug].js to posts folder instead of blog but it's more tricky for it to be at the root and would need quite a bit of modification to make it work.

Alternatively, if you are porting from hugo, you can add a redirect from url/sample-page to url/blog/sample-page. The documentation covers it in detail - https://nextjs.org/docs/api-reference/next.config.js/redirects

@expatjedi
Copy link
Author

The problem is that Google already indexed my old blog posts so when the visitor try to open my new site it will show 404 error. That's why i wanted to move posts in root pages directory. I will search another solution on next.js community. I hope they have a solution for this.

I also recommend you to make this modifications on your template since people who decided shifting to your theme would definitely search the same solution. Serving pages under /blog directory is not much practical.

@DoctorDerek
Copy link
Contributor

Hey @koraylinux I think it's pretty straightforward to do what you want:
image
from https://nextjs.org/docs/routing/introduction#dynamic-route-segments

At least for me, it just works out-of-the-box by moving the [slug].js file from pages/posts/[slug].js to pages/[slug].js... other than updating some of the links in the rest of the code.
image

@timlrx
Copy link
Owner

timlrx commented Apr 18, 2021

That's what redirects do - it redirects users from the original link to the new link without 404 errors. My old blog uses the url/year/month/day/slug format and I re-directed them to url/blog/slug page for the new one so there are no such errors.

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

3 participants