hexo-theme-next icon indicating copy to clipboard operation
hexo-theme-next copied to clipboard

Generated wrong post link when website is in a subdirectory [solved]

Open hyperscalar opened this issue 8 years ago • 17 comments

I agree and want to create new issue


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: c

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

  1. N/A
  2. N/A
  3. 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

hyperscalar avatar Nov 10 '18 15:11 hyperscalar

root: /en/ – not need.

ivan-nginx avatar Nov 11 '18 03:11 ivan-nginx

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.

hyperscalar avatar Nov 11 '18 06:11 hyperscalar

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.

chaolinhan avatar Nov 12 '18 16:11 chaolinhan

@sli1989 This may be an issue image image url_for() is used twice.

jiangtj avatar Nov 13 '18 08:11 jiangtj

Yes, please, check it. May be bug.

ivan-nginx avatar Nov 15 '18 16:11 ivan-nginx

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

hyperscalar avatar Nov 26 '18 12:11 hyperscalar

Ok, i'll check it later. Please, wait. And thank's for report.

ivan-nginx avatar Nov 29 '18 11:11 ivan-nginx

It seems the same problem with #231, caused by url_for().

stevenjoezhang avatar Dec 02 '18 03:12 stevenjoezhang

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.

hyperscalar avatar Dec 02 '18 09:12 hyperscalar

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

Same issue, change attrs can not solve problem

Levyhehh avatar Dec 06 '18 02:12 Levyhehh

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

I fixed that, seems alright. change var attrs = { href: path.indexOf(config.root) !== -1 ? path : this.url_for(path) };

Levyhehh avatar Dec 06 '18 02:12 Levyhehh

So, this bug only for subdirectory? If will not subdirectory, fix will work fine?

ivan-nginx avatar Dec 06 '18 02:12 ivan-nginx

Yes, this bug shouldn't appear if site were not in subdirectory.

hyperscalar avatar Dec 06 '18 02:12 hyperscalar

@ivan-nginx image image Some use next_url(url_for()), but some next_url(), it maybe reason. commint sha:819e1a5d38fa1a1e9a4f5a5ac10e57530d339b36

jiangtj avatar Dec 06 '18 02:12 jiangtj

Maybe need to use just next_url()?

ivan-nginx avatar Dec 06 '18 02:12 ivan-nginx

Should be fixed in #496

stevenjoezhang avatar Dec 10 '18 13:12 stevenjoezhang

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.

lock[bot] avatar Mar 10 '20 20:03 lock[bot]