Saturday, June 14, 2008

Using rails routing for easy search engine friendly URL changes


In this entry I am going to show how to, in a very search engine friendly way, update a URL for a particular page in a rails website. The rails routing system provides named routes, which are a very nice way to change the URL for a particular page and have all of your links update without having to change code in a bunch of different places. That is a great start on moving a page, but it doesn't take care of any links from outside of your site that might be pointing at the old URL. The best way to handle that is to make it so that the old URL redirects to the new URL, your end users won't notice a difference but the search engines bots will take note and update their indexes.


First off if you are not familiar with routing and named routes in rails you might want to read up on it. The rails API docs have some pretty good material on routes located at http://api.rubyonrails.org/classes/ActionController/Routing.htm


Lets pretend we are moving "http://example.com/users/signup" to "http://example.com/signup". Our original named route might look something like this:


map.signup '/users/signup', :controller => 'users', :action => 'signup'


And the code for a link that pointed to it would look something like this:


<%= link_to 'Signup', signup_url %>

To change our URL we would simply change the named route to look like this:

map.signup '/signup', :controller => 'users', :action => 'signup'

All of the links that used the named route would automatically change to link to the new URL but the old URL would stop working. To change that we could add the following to the routes file

map.signup_old '/users/signup', :controller => 'users', :action => 'signup', :old_url => true

This would allow the old URL to work, but it would be best if we made it so that it redirected to the new URL so that search engines would switch over to the new URL faster. Notice that there is an additional parameter that is defined on the end of the second route, this makes it so that we can easily differentiate what URL a visitor arrived on the page with.

Now all we have to do is make a method we can use in a before filter that redirects the user to the same action, the redirect will automatically use the first route we defined (as long as we leave them in that order in the routes file). In the users controller we would add the following code:

before_filter :redirect_to_new_url

def redirect_to_new_url
if params[:old_url] == true
headers["Status"] = "301 Moved Permanently"
redirect_to({:action => action_name})
end
end
The lines in the route file would look like this:

map.signup '/signup', :controller => 'users', :action => 'signup'
map.signup_old '/users/signup', :controller => 'users', :action => 'signup', :old_url => true

Once you have this in place you only have to add a new route to move a URL in a search engine friendly way, and when you decide that all the external links and search engines are updated all you have to do is get rid of the route that points to the old URL.

23 comments:

Abdul Bari Chanessra said...

Memphis SEO training classes and seminars in Memphis, Tennessee from local training companies.

Victoria John said...

SEO Course in Chennai

Thanks for sharing this information. SEO is one of the digital marketing techniques which is used to increase website traffic and organic search results. If anyone wants to get SEO Training in Chennai visit FITA Academy located at Chennai. Rated as No.1 SEO Training institute in Chennai.

SEO Training in Chennai

jack wilson said...

Thanks for sharing this informative blog. I have read your blog and I gathered some valuable information from this blog. Keep posting. Recently I did Digital Marketing Courses in Chennai at a leading digital marketing company. It's really useful for me to make a bright career.

Sathik Ali. A said...

Thanks for very informative blog excellent information about SEO Training. I am planning to do SEO Training in Chennai. Please suggest me for best institute for SEO Training Institute in Chennai. I heard IICT Chromepet is best institute for SAP Training in Chennai what about SEO Training.

raju kr said...

EMTV commenced broadcasting within the National Capital District of Papua New Guinea in July 1987.A transmission Centre was opened in Lae in April 1988 with Mt Hagen, Goroka, Arawa and Rabaul following in rapid succession. A milestone was reached in June 1989 when EMTV commenced being received live in Lae, Mt Hagen and Goroka.Twenty years later EMTV is now received in real time via satellite in 38 centres throughout Papua New Guinea.
PNG Entertainment News

Al Waller said...

Your website is beautifully decorated and easily navigated. I have enjoyed visiting this site today and hope to visit many more times in the future. If any one want to get more about Memphis local SEO service you may visit Memphis SEO .

john son said...

SAP is one of the customer relationship management software and it support all end to end customer related process. To know more details about SAP modules please refer the following site.
Regards..
SAP Training Chennai

Robert Smith said...
This comment has been removed by the author.
Andria BZ said...

Thanks for sharing this niche useful informative post to our knowledge, Actually SAP is ERP software that can be used in many companies for their day to day business activities it has great scope in future.
Regards,
SAP training in chennai|SAP Course in Chennai

faizan qureshi said...

We enjoy your feedback. As opposed to problems, Personally i think a person sounded sensible. A nice career, in which such as every other, Seo Baclinks Blog Commenting Service And Seo Services..

Roshini RS said...


Hi Admin,
Thanks for taking time to discussing about SAP, i was interesting to read from the beginning, keep your updates regularly...
Regards,
SAP MM Training In Chennai|SAP HANA Training in Chennai|sap training in Chennai

Akula Rahul said...

Jharkhand Labour Department Recruitment 2016

Good post. This is a very clear, informative and helpful post.......

Dustin Maffei said...

Thanks for sharing this information. Please visit Bangladeshi Americans classified .

Jeanne Davies said...

Really like it so much thanks for sharing amazing info..:) ecommerce web design company

Kayal Vizhi said...

Nice blog. happy to visit your blog. Thanks for sharing.


Digital Marketing Services in Chennai

Regina Rj said...

Good post. Thanks for sharing such a informative post.

PPC training in chennai

Praveen Kumar said...

This post has actually made me your fans and I have added your site to my favorites.SAP Simple Finance Training

Arjun Rishi said...

Great article! Glad to find your blog. keep sharing.

digital marketing training in chennai

Rohit P said...

That was really great information on this blog , very interesting to read them ,
Thanks got to know more

Thanks for share

learn digital marketing and seo from our experts digital marketing training in bangalore | seo training in bangalore join to know more on it

Nivedageorge said...

Thanks for sharing such an informative post. Keep update more.
Digital Marketing Training In Chennai

SKARtec Digital Marketing Academy said...

Your blog is really useful for me. Thanks for sharing this informative blog.

The most advanced digital marketing training in Chennai for working professionals, business owners and job seekers - SKARtec Digital Marketing Academy

for IT the said...

I have read your blog its very attractive and impressive. I like it your blog.

Google Search Engine Optimization Services Guaranteed SEO Company in India

SEO Company in India SEO Services in India

ERP said...

This post is very informative for everybody. I would like to appreciate your work for good
accuracy and got informative knowledge from here. Nice effort, very informative, this will
help me to complete my task. Thanks for share it keep it up.



Oracle Fusion HCM Training