rust-lang / mdBook

Create book from markdown files. Like Gitbook but implemented in Rust
https://rust-lang.github.io/mdBook/
Mozilla Public License 2.0
17.96k stars 1.62k forks source link

mdBook .htaccess? #1794

Closed ghost closed 2 years ago

ghost commented 2 years ago

Is there any way to implement a .htacces file that can be used to negate the .html proceeding after the URL? I assumed that the 404 was handled by a .htaccess file but that seems to be handled through something else (presumably JS). Which leads back to my question; is there a way to remove the .html?

ehuss commented 2 years ago

I don't know if there is a way to rewrite the URLs to remove the .html extension. You may need to look into your webserver's configuration if that is possible, but I think mdBook's linkifier will make that difficult. I believe this is mostly a duplicate of #1563, so closing in favor of that.

sspaeti commented 1 year ago

I had similar questions. I guess we could do something like this in .htaccess (Apache):

RewriteEngine On

# If the request ends with a /, try to find the corresponding .html file
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
RewriteRule ^(.*[^/])/$ $1.html [L]

This rule works as follows:

  1. The RewriteCond checks if the .html file corresponding to the directory-like URL exists.
  2. If it exists, the RewriteRule will rewrite the request to the .html file.

For example, with this rule:

As for performance concerns:

But I'd rather do something like this https://github.com/rust-lang/mdBook/issues/1563, I'm not sure if that would be a long-term solution 🤔.