Generated wrong post link when website is in a subdirectory [solved]
I agree and want to create new issue
- [X] Yes, I was on Hexo Docs page, especially on Templates, Variables, Helpers and Troubleshooting.
- [X] Yes, I was on NexT Documentation Site.
- [X] And yes, I already searched for current issues and this is not help to me.
Expected behavior
My site is running in a subdirectory, for an instance: xxx.github.io/en Every link of post title should be something like xxx.github.io/en/posts/{Post-Title}, and every read more button should have the same link as the post title.
Actual behavior
After I update the latest release of the theme, every link of post title becomes xxx.github.io/en/en/posts/{Post-Title}, so that's a duplicate of my subdirectory name and caused 404. However, the read more button still has the right link to a post like xxx.github.io/en/posts/{Post-Title}
This is a part of my hexo configuration:
# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: https://bitrhythm.github.io/en
root: /en/
permalink: posts/:title/
permalink_defaults:
This is the screenshot:
This is the html source:
Post title link:
<a href="/en/en/posts/Hello-World/" class="post-title-link" itemprop="https://bitrhythm.github.io/en/index.html">Hello World</a>
Read more link:
<a class="btn" href="/en/posts/Hello-World/#more" rel="contents"> Read more » </a>
Steps to reproduce the behavior
- N/A
- N/A
- N/A
- Link to demo site with this issue: N/A
- Link(s) to source code or any usefull link(s): N/A
Node.js and NPM Information
Package dependencies Information
Hexo Information
Hexo version
Hexo Configuration
NexT Information
NexT Version:
- [ ] Latest Master branch
- [ ] Latest Release version
- [ ] Old version
NexT Scheme:
- [ ] All schemes
- [ ] Muse
- [ ] Mist
- [ ] Pisces
- [ ] Gemini
NexT Configuration:
Other Information
root: /en/ – not need.
root: /en/– not need.
Website in subdirectory
If your website is in a subdirectory (such as http://example.org/blog) set url to http://example.org/blog and set root to /blog/.
But this is what the hexo docs says. And the read more button actually has the right link, so I'd rather believe this is not the real cause of the problem.
I've also got this problem in updating to NexT 6 from NexT 5.1.3. In NexT 5 the post links are correctly generated, but with the same configuration in NexT 6 links are generated wrongly with subdirectory repeating twice as described above.
@sli1989
This may be an issue
url_for() is used twice.
Yes, please, check it. May be bug.
I changed var attrs = { href: this.url_for(path) }; to var attrs = { href: path }; in scripts/helpers/next-url.js, and then the problem solved...thanks to @JiangTJ
But I'm not sure this is the right solution, so @ivan-nginx please check it.
Update: this solution will cause all navigations links to lose their subdirectory name, /subdirectory/categories become /categories
Ok, i'll check it later. Please, wait. And thank's for report.
It seems the same problem with #231, caused by url_for().
Update: I tried changing relative_link: false -> true in hexo's _config.yml file, this DID solve the problem of the post link but caused a new problem that ALL NAVIGATION LINK becomes unclickable, in other words, all navigation link tag lost its value of href attribute.
Hope this gave you guys helpful information.
I changed
var attrs = { href: this.url_for(path) };tovar attrs = { href: path };in scripts/helpers/next-url.js, and then the problem solved...thanks to @JiangTJ But I'm not sure this is the right solution, so @ivan-nginx please check it.Update: this solution will cause all navigations links to lose their subdirectory name, /subdirectory/categories become /categories
Same issue, change attrs can not solve problem
I changed
var attrs = { href: this.url_for(path) };tovar attrs = { href: path };in scripts/helpers/next-url.js, and then the problem solved...thanks to @JiangTJ But I'm not sure this is the right solution, so @ivan-nginx please check it.Update: this solution will cause all navigations links to lose their subdirectory name, /subdirectory/categories become /categories
I fixed that, seems alright.
change var attrs = { href: path.indexOf(config.root) !== -1 ? path : this.url_for(path) };
So, this bug only for subdirectory? If will not subdirectory, fix will work fine?
Yes, this bug shouldn't appear if site were not in subdirectory.
@ivan-nginx
Some use next_url(url_for()), but some next_url(), it maybe reason.
commint sha:819e1a5d38fa1a1e9a4f5a5ac10e57530d339b36
Maybe need to use just next_url()?
Should be fixed in #496
This thread has been automatically locked since there has not been any recent activity after it was closed. It is possible issue was solved or at least outdated. Feel free to open new for related bugs.