2015-12-12 68 views
0

我添加以下爲了我config.rb文件內線擺脫.html延長所以http://example.com/about.html變得http://example.com/about並從build文件夾中找我可以看到這是爲每個文件創建一個單獨的目錄,其名稱爲about/index.html,用於about.html頁面。這意味着如果用http://example.com/about.html訪問網站,則不會找到這樣的網頁,我期望發生的是重定向到http://example.com/about,或者至少爲相關頁面提供服務並保持網址不被清理。中間人刪除擴展名爲.html,但保持http://example.com/about.html有效

回答

0

我相信要找到你要找的東西的唯一方法就是通過你的網絡服務器解決這個問題。一個選項:

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> 
+0

只是附加的註釋。有example.com/about.html重定向到example.com/about是不可能通過中間人,因爲中間人只在本地使用ruby生成一個靜態網站,您將上傳到您的服務器。這就是nginx會處理.html擴展名的地方,如果需要的話,可以將301從about.html重定向到about。然而,如果你更關心索引正確頁面的搜索引擎,我認爲更好的選擇是不做301,因爲根據你的網站有多大,這可能會變得混亂,使用規範的URL可能效果最好。 –