-
-
Save tisho/269784 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'uri' | |
def generate_canonical_link | |
query_elements = [] | |
query_elements << "page=#{params[:page]}" unless params[:page].to_i <= 1 # page=0 should be valid? | |
query_elements << "query=#{params[:query]}" if params[:query] | |
# use URI::Generic.build2 to automatically escape the query params | |
# you can actually get away with simple string manipulation here, but you get the idea | |
uri = URI::Generic.build( :scheme => request.env['rack.url_scheme'], | |
:host => SERVICE_DOMAIN, | |
:path => request.env['REQUEST_PATH'], | |
:query => query_elements.join('&')) | |
# chop off trailing '?' and return link | |
%(<link rel="canonical" href="#{uri.to_s.chomp '?'}" />) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment