Seo Friendly Urls in Ror

SEO friendly URLs in RoR

Check out friendly_id gem. It is exactly what you need.

Local language and SEO friendly urls in Ruby on Rails 4

You can use route_translator gem for this task.

You have just to wrap your routes you want to be localized in a localized block, and add translations to your locale files.

For friendly urls, you can use friendly_id.

Edit:
Considering your comment, you can use path in your routes:

resources :users, path: 'betnuzer', path_names: { new: 'schaffen', edit: 'bearbeiten' }

Then you can use users_path, etc in your backend, and your routes are translated.

how do I make the URL's in Ruby on Rails SEO friendly knowing a @vendor.name?

All of these solutions use find_by_name, which would definitely require having an index on that column and require they are unique. A better solution that we have used, sacrificing a small amount of beauty, is to use prefix the vendor name with its ID. This means that you dont have to have an index on your name column and/or require uniqueness.

vendor.rb

def to_param
normalized_name = name.gsub(' ', '-').gsub(/[^a-zA-Z0-9\_\-\.]/, '')
"#{self.id}-#{normalized_name}"
end

So this would give you URLs like

/1-Acme

/19-Safeway

etc

Then in your show action you can still use

Vendor.find(params[:id])

as that method will implicitly call .to_i on its argument, and calling to_i on such a string will always return the numerical prefix and drop the remaining text- its all fluff at that point.

The above assumes you are using the default route of /:controller/:action/:id, which would make your URLs look like

/vendors/show/1-Acme

But if you want them to just look

/1-Acme

Then have a route like

map.show_vendor '/:id', :controller => 'vendors', :action => 'show'

This would imply that that it would pretty much swallow alot of URLs that you probably wouldnt want it too. Take warning.

Rails SEO friendly URL

Add a route to your routes.rb file which includes the tit paramter directly in the path.

For example:

Rails.application.routes.draw do
get 'post/:tit' => 'posts#show', as: :blog_post
end

Of course you have to adjust the routes to your needs.

Just have a short look into this question. It doesn't matter if you have parameters in the URL anymore.

Rails - SEO friendly URLs with Self joined models

Creating routes which use another attribute than the ID is pretty trivial. FriendlyID a great out of box solution but it does will only get you to URLs that look like:

/categories/lamas/categories/maintenance/products/lama-polish

Creating URL's such as lamas/maintenence/lama-polish is definitely possible but will be difficult since its not conventional and there are many potential pitfalls.

You could for example start out with:

resources :categories, path: '/' do
resources :categories, path: '' do
# resources :products
end
end

Which will create:

                Prefix Verb   URI Pattern                      Controller#Action
category_categories GET /:category_id(.:format) categories#index
POST /:category_id(.:format) categories#create
new_category_category GET /:category_id/new(.:format) categories#new
edit_category_category GET /:category_id/:id/edit(.:format) categories#edit
category_category GET /:category_id/:id(.:format) categories#show
PATCH /:category_id/:id(.:format) categories#update
PUT /:category_id/:id(.:format) categories#update
DELETE /:category_id/:id(.:format) categories#destroy
categories GET / categories#index
POST / categories#create
new_category GET /new(.:format) categories#new
edit_category GET /:id/edit(.:format) categories#edit
category GET /:id(.:format) categories#show
PATCH /:id(.:format) categories#update
PUT /:id(.:format) categories#update
DELETE /:id(.:format) categories#destroy

But then there is a supersized gotcha - let's say the request is for

GET /lamas/joe-the-lama

How is Rails supposed to know that this request should be handled by LamasController and not CategoriesController? You would have to do a database query for both. Of course this is not an issue if you always have two categories but you get my drift - things are going to get complicated fast.

The standard Rails style restful routing may be a bit wordy but it does avoid a lot of potential ambiguities.

Supporting URLs like /similar-to-:product in Ruby on Rails?

In fact you can add - as a separator, then use route globbing.

map.similar_product '/similar-to-*product', :controller => 'products', :action => 'similar'

then, in ProductsController#similar

@product = Product.find_by_slug params[:product].join('-')

Though refactoring does seem nicer, since with this approach you'll need to specially handle all slugs that can contain hyphens.



Related Topics



Leave a reply



Submit