Hexo: Get rid of index.html in URL

Created on 31 May 2015  ·  18Comments  ·  Source: hexojs/hexo

Is there any way to remove index.html from all links?

enhancement feature-request pending-reply

Most helpful comment

I ended up with a helper:

hexo.extend.helper.register('page_url', function(path, options) {
    return this.url_for(path, options).replace(/index\.html$/, '');
});

But internal settings would be better.

All 18 comments

I noticed this also. I edited the theme. That's how I 'sanitized' the path.

- var regex = /(\/index\.html?$)|(\/?$)/;
- var path = article.path.replace(regex, '');
//-fix for permalink in pages
a(href=url_for(path))
  +title(article)

This feature is good to have.

I ended up with a helper:

hexo.extend.helper.register('page_url', function(path, options) {
    return this.url_for(path, options).replace(/index\.html$/, '');
});

But internal settings would be better.

I'm using this setting and not seeing index.html in the site generated.
How are you guys producing this issue?

permalink: :title/

I gave up with Hexo, so it’s irrelevant for me now.

How are you guys producing this issue?

@leesei this happens on pages, not posts.

See also: #1387 (duplicate?)

Come on guys, what's up with this? I can't believe it's 8 months since last thought (@sapegin quit) and nothing's been fixed?
@sapegin - what are you using now? I just came to hexo because of metalsmith problems, and things continue to go wrong... I'm getting very tired now! :)

@fourpixels I’ve written my own simple generator and now I’m finally happy ;-)

Hey, it looks interesting! Unfortunately I cannot use it right now as I'm in a big hurry and I need out of the box solutions for posts querying, categories, tags and all that crap :(
But it seemed like almost every tool I use has it's own drawbacks. Not only that I need to hack .htaccess file to remove the crappy .index, but I also have to manually copy that file around! (swear)

Good luck with your generator!

@fourpixels why not setting a helper, just like @sapegin did?

@mef I already set permalink: :title so it works well. I guess I'm just overwhelmed with all the little things I need to fix just to get it working. I still had to add that .htaccess or my /public/about.html won't be opened from /about/ ;)

post.permalink still have "index.html", so plugins that use that variable needs to remove it manually (e.g. https://github.com/hexojs/hexo/pull/3661 https://github.com/hexojs/hexo-generator-sitemap/pull/59). I wonder if it's safe to remove it directly from the source.

I wonder if it's safe to remove it directly from the source.

Google crawler seems that sees these as duplicate. I think it would be better if we decide which use in all of hexo source code.

Or add a new option for a user can select by themselves.

If you have a single page accessible by multiple URLs, or different pages with similar content (for example, a page with both a mobile and a desktop version), Google sees these as duplicate versions of the same page. Google will choose one URL as the canonical version and crawl that, and all other URLs will be considered duplicate URLs and crawled less often.

I do worry having index.html may negatively affect SEO. Just to clarify, what I meant is removing "index.html" from the source of "permalink" variable,

https://github.com/hexojs/hexo/blob/651f34bf6d2be5cb9c07009e857a73ecad3b414c/lib/models/post.js#L52-L58

by adding following line after line 56:

partial_url = partial_url.replace(/index\.html$/, '');

I concur on having an option. something like,

if (config.canonical_url) {
   partial_url = partial_url.replace(/index\.html$/, '');
}

what I meant is removing "index.html" from the source of "permalink" variable

How about pages using another file name, like about.html, for which the canonical url should be /about/?

you mean when you set

permalink: :year/:month/:day/:title.html

just like https://github.com/theme-next/hexo-theme-next/issues/866?

The main concern of this PR is that permalink could be (I haven't try) "http://example.com/2018/08/20/test-title.html/index.html", the index.html shouldn't be there.

If that config is set, then permalink should be "http://example.com/2018/08/20/test-title.html", as configured.

As for <link rel="canonical" href="http://example.com/2018/08/20/test-title/">, AFAIK the tag is not added by hexo. User can utilize helper function to insert the canonical tag.

hexo.extend.helper.register('canonical_tag', () => {
  return '<link rel="canonical" href="' + post.permalink.replace(/\.html$/, '/') + '">';
});

Note the above codes needs to be refactored into this format to access post.permalink variable.

The main concern of this PR is that permalink could be (I haven't try) "http://example.com/2018/08/20/test-title.html/index.html", the index.html shouldn't be there.

Indeed, I was confused about the problem, you may disregard previous comment, thanks.

Was this page helpful?
0 / 5 - 0 ratings