fvsch/kirby-staticbuilder

URL issue for top-level pages

Closed this issue · 12 comments

My website utilizes subpages for each top-level page. Whenever I build my site, all my top-level pages take the URL http://localhost/section.html, while my subpages take the URL http://localhost/section/subpage.html. I want the section index pages to take the form http://localhost/section/index.html. Since this would appear to be a common issue and I don't see any documentation of it, I'm afraid I might be missing something. For now I am manually moving the generated pages to the proper folder and renaming them index.html. Any help is appreciated. Thanks.

fvsch commented

Hi,

StaticBuilder has the staticbuilder.extension config:

c::set('staticbuilder.extension', '.html');       // my/page.html
c::set('staticbuilder.extension', '/index.html'); // my/page/index.html

If you use '/index.html', you will get the result you expect for section pages. But it will also apply to subpages, so you will get static/section/subpage/index.html and not static/section/subpage.html. There is no built-in way to treat those pages differently.

So your options are:

  1. c::set('staticbuilder.extension', '.html'); /* default */
    static/section.html and static/section/subpage.html

  2. c::set('staticbuilder.extension', '/index.html');
    static/section/index.html and static/section/subpage/index.html

With the appropriate server configuration, the first one gives you URLs that look like this: /section and /section/subpage; and the second one gives you URLs that look like: /section/ and /section/subpage/.

Ok, thanks. I did add Options +Multiviews to the .htaccess file as suggested, but that didn't cause /section/ URLs to open the section.html pages. Instead, they opened the index of the folder. Do you know of any Apache headers that would fix this?

fvsch commented

That would be the Indexes option.

Options -Indexes +Multiviews

Note that it will disable folder listings but it will not fix your issue, because you’re requesting /section/, which cannot map to [WEB ROOT]/section.html. So if you disable the folder listing (which you should do anyway), you will get a 404.

You need to pick a URL scheme:

/section
/section/subpage

or:

/section/
/section/subpage/

The first one should work out of the box with Kirby and StaticBuilder's default options. The second one can be trickier because Kirby uses /page/subpage for URLs. If you want trailing slashes, you have to put them explicitely in your templates, e.g.

<a href="<?= $somePage->url() ?>/">
  <?= $somePage->title() ?>
</a>

Hmm. For some reason, even with -Indexes set, my server wants to redirect /section.html to /section/. My links all have URLs without the trailing slash. I'm on MediaTemple's Grid.

Incidentally, I'm having some issues linking embedded image files. I'm using Kirbytext syntax (image: infographic.jpg link: infographic.jpg) to have an image open itself when clicked, yet the URLs are coming out wrong. I have staticbuilder.baseurl set to '/2016/'. I had to add a trailing slash to get my URLs to come out right, though your docs suggest I don't need it. Here is the site, for reference, and here is an example of the linked image embed not working.

fvsch commented

@zakgreene I can reproduce the issue. Problem is that if you have these files:

static/my-section.html
static/my-section/subpage.html

And request /my-section, Apache will try to serve the directory and not the HTML page with the same name.

There are a few solutions using a .htaccess and URL rewriting. I’m trying a few things to figure out what could be a decent default config.

Yep, that's exactly it. Thanks for working on it.

fvsch commented

One possible fix is using a .htaccess file like this one:

https://github.com/fvsch/kirby-staticbuilder/blob/master/test/static_htaccess

Which you could save as e.g. site/staticbuilder/htaccess, and copy it on build:

c::set('staticbuilder.assets', [
  'assets' => 'assets',
  'content' => 'content',
  'thumbs' => 'thumbs',
  'site/staticbuilder/htaccess' => '.htaccess'
]);

It’s a bit of a heavy solution, so I’m considering changing the default extension to /index.html in the next major version of StaticBuilder.

Do you know of a way to make it work if the entire site is in a subfolder? The only way I can think of now is to add Redirect /index.html /2016/index.html (where /2016 is my subfolder), but then I'd have to change all my homepage URLs to http://localhost/2016/index.html.

fvsch commented

Do you know of a way to make it work if the entire site is in a subfolder?

Can you elaborate what you mean by “make it work” in that sentence?

With the settings you provided, links to the homepage don't work when the entire website uses a subfolder. I either have to link directly to subfolder/index.html, or move the homepage to the root directory and name it subfolder.html. Neither of these is ideal. I don't know much about .htaccess, so I wondered whether there was a way to exempt /subfolder from the rewrite rules.

fvsch commented
# Redirect "/something/" to "/something"
# Warning: only enable if serving the site at the root of a domain or subdomain,
# e.g. https://my.site.com/ or http://localhost/
# Otherwise it will redirect "/site/root/" to "/site/root", which can be a problem.
RedirectMatch (.+?)/+$ $1

You probably want to comment or remove that section.

This worked for me — sorry for the late reply.