Middleman删除.html扩展名,但保留http://example.com/about.html有效的



我在config.rb文件中添加了以下行,以便去掉.html的扩展名,使http://example.com/about.html变成http://example.com/about。从build文件夹中查看,我可以看到这是为每个文件创建一个单独的目录,并将其名称命名为about.html页面的about/index.html。这意味着,如果使用http://example.com/about.html访问网站,将找不到这样的页面,我希望发生的是重定向到http://example.com/about,或者至少提供相关页面并保持url未清理。

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

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(如果您想使用规范url)

<!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>

相关内容

最新更新