remove trailing slash and redirect (SEO)

This commit is contained in:
did 2011-06-22 08:53:29 -07:00
parent a5cd2290ea
commit 40530de8bb
8 changed files with 58 additions and 4 deletions

View File

@ -47,5 +47,6 @@ module Locomotive
config.filter_parameters << :password
config.middleware.insert_after Rack::Lock, '::Locomotive::Middlewares::Fonts', :path => %r{^/fonts}
config.middleware.insert_after Rack::Lock, '::Locomotive::Middlewares::SeoTrailingSlash'
end
end

View File

@ -33,7 +33,7 @@ unless Locomotive.engine?
begin
require 'rack/cache'
Rails.application.middleware.insert_before 'Dragonfly::Middleware', 'Rack::Cache', {
:verbose => true,
:verbose => false,
:metastore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/meta"), # URI encoded in case of spaces
:entitystore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/body")
}

View File

@ -21,7 +21,9 @@ x contents permalink (UI)
x BUG: has_one / has_many. Delete an author
x bushido changes in the master
? edit sidebar (inline editor). Unable to reset it
- SEO: support and support/ should be 2 different pages. Remove trailing slash
x SEO: support and support/ should be 2 different pages. Remove trailing slash
- Httparty (issue #...)
- Has_one => group by in the select
BACKLOG:

View File

@ -35,6 +35,7 @@ module Locomotive
initializer "serving fonts" do |app|
app.middleware.insert_after Rack::Lock, '::Locomotive::Middlewares::Fonts', :path => %r{^/fonts}
app.middleware.insert_after Rack::Lock, '::Locomotive::Middlewares::SeoTrailingSlash'
end
end

View File

@ -1 +1,2 @@
require 'locomotive/middlewares/fonts'
require 'locomotive/middlewares/seo_trailing_slash'

View File

@ -0,0 +1,24 @@
module Locomotive
module Middlewares
class SeoTrailingSlash
def initialize(app, opts = {})
@app = app
end
def call(env)
path = env['PATH_INFO']
if !path.starts_with('/admin/') && (match = path.match(%r{(.+)/$}))
response = Rack::Response.new
response.redirect(match[1], 301) # moved permanently
response.finish
response.to_a
else
@app.call(env)
end
end
end
end
end

View File

@ -85,7 +85,7 @@ describe 'Locomotive rendering system' do
@controller.send(:locomotive_page).should be_true
end
context 'redirect page' do
context 'redirect' do
before(:each) do
@page.redirect = true

View File

@ -0,0 +1,25 @@
require 'spec_helper'
describe Locomotive::Middlewares::SeoTrailingSlash do
it 'does not process the "/" url' do
get '/'
response.status.should_not be(301)
end
it 'does not process the "/admin/" url' do
get '/admin/'
response.status.should_not be(301)
end
it 'does not process the "/admin/*" urls' do
get '/admin/login'
response.status.should_not be(301)
end
it 'redirects to the url without the trailing slash' do
get '/hello_world/'
response.status.should be(301)
end
end