Middleman删除.html扩展名,但保持http://example.com/about.html有效(Middleman remove .html extension, but keep http://example.com/about.html valid)

我在config.rb文件中添加了以下行,以便摆脱.html扩展名,以便http://example.com/about.html成为http://example.com/about并从内部查看build文件夹,我可以看看它的作用是为每个文件创建一个单独的目录,其名称为about.html page的about/index.html 。 这意味着如果使用http://example.com/about.html访问网站,则无法找到此类页面,我希望发生的是重定向到http://example.com/about或至少提供服务相关页面并保持网址不清除。

I added following line inside my config.rb file in order to get rid of .html extension so http://example.com/about.html becomes http://example.com/about and from looking inside a build folder I can see that what this does is create a separate directory for each file with its name so about/index.html for about.html page. This means that if website is accessed with http://example.com/about.html such page will not be found and what I'd expect to happen is a redirect to http://example.com/about or at least serve relevant page and keep url uncleaned.

最满意答案

我相信获得你想要的东西的唯一方法是通过你的网络服务器解决这个问题。 一种选择:

config.rb

require 'rack/middleman/optional_html' use Rack::OptionalHtml, root: '/example_domain/source', urls: %w[/]

的Gemfile

gem 'optional_html', :git => 'https://github.com/tommysundstrom/middleman-rack-optional-html.git'

_nav.html.erb

<nav> <a href="/about">About</a> </nav>

example.com.conf (将此添加到您的nginx服务器块)

location / { try_files $uri.html $uri/index.html /fallback.html; } location = /fallback.html { }

layout.erb (如果你想使用规范网址)

<!DOCTYPE html> <html lang="en"> <head> <title><%= current_page.data.title %></title> <link rel="canonical" href="https://www.example.com/<%= current_page.path.chomp('index.html').chomp('.html').chomp('/') %>"/> </head>

I belive the only way to get what you're looking for is by tackling this through your web server. One option:

config.rb

require 'rack/middleman/optional_html' use Rack::OptionalHtml, root: '/example_domain/source', urls: %w[/]

Gemfile

gem 'optional_html', :git => 'https://github.com/tommysundstrom/middleman-rack-optional-html.git'

_nav.html.erb

<nav> <a href="/about">About</a> </nav>

example.com.conf (add this to your nginx server block)

location / { try_files $uri.html $uri/index.html /fallback.html; } location = /fallback.html { }

layout.erb (If you wanted to use canonical urls)

<!DOCTYPE html> <html lang="en"> <head> <title><%= current_page.data.title %></title> <link rel="canonical" href="https://www.example.com/<%= current_page.path.chomp('index.html').chomp('.html').chomp('/') %>"/> </head>

更多推荐