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.

77 comments:

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

    ReplyDelete
  2. 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

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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

    ReplyDelete
  6. 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 .

    ReplyDelete
  7. 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

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. 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..

    ReplyDelete

  10. 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

    ReplyDelete
  11. Jharkhand Labour Department Recruitment 2016

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

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

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

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


    Digital Marketing Services in Chennai

    ReplyDelete
  15. Good post. Thanks for sharing such a informative post.

    PPC training in chennai

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

    ReplyDelete
  17. 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

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

    ReplyDelete
  19. Thanks for sharing the useful information and good points were stated in this article which is very informative and for the further information visit our site
    Oracle Fusion Financials Training

    ReplyDelete
  20. Hi,
    Thanks for taking time to discussing about SAP, i was interesting to read from the beginning, keep your updates regularly.people who interested to learn oracle and its suite just visit oue site for more info. Oracle EBS training
    Thank you.

    ReplyDelete
  21. Your blog is extremely valuable for me. A debt of gratitude is in order for sharing this instructive blog.
    Read Beard Press

    ReplyDelete
  22. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    Digital marketing course in chennai


    ReplyDelete
  23. I am really admired for the great info is visible in this blog that to lot of benefits for visiting the nice info in this website. Thanks a lot for using the nice info is visible in this blog.
    Java training in chennai | Data Science Training in Chennai | DevOps Training in Chennai

    ReplyDelete

  24. Leading Mobile Service Centre in Chennai, We repair all models of smart phones like Samsung, Apple, Motorola, Lenovo, Redmi, Honor, Oneplus, Oppo, Coolpad, Htc and many other models.

    Mobile Service Center in Chennai | Mobile Service Centre | Mobile Service Center | Mobile Service Center near me | Mobile Service Center in velachery | Mobile Service Chennai

    ReplyDelete

  25. Thanks for sharing the good information and post more information. Talent flames company is one of the best training and placement companies in Hyderabad. Providing training on Technologies like Java,Sql,Oracle,..,etc with 100% Placement Assistance. Now Interviews going on if you want to attend just visit our website and drop your resume. for more information visit us http://talentflames.com/
    training and placement company in Hyderabad

    ReplyDelete
  26. I like the post format as you create user engagement in the complete article. It seems round up of all published posts. Thanks for gauging the informative posts. "https://www.dynamicslabs.io"

    ReplyDelete
  27. I like the post format as you create user engagement in the complete article. It seems round up of all published posts. Thanks for gauging the informative posts.Microsoft Dynamics 365

    ReplyDelete
  28. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.
    ExcelR Data Science Course

    ReplyDelete
  29. Going to graduate school was a positive decision for me. I enjoyed the coursework, the presentations, the fellow students, and the professors. And since my company reimbursed 100% of the tuition, the only cost that I had to pay on my own was for books and supplies. Otherwise, I received a free master’s degree. All that I had to invest was my time.

    Data Science Certification Malaysia
    Big Data Analytics Malaysia
    Data Analytics Course Malaysia

    ReplyDelete
  30. Cool you write, the information is very good and interesting, I'll give you a link to my site.
    big data in malaysia
    data scientist course malaysia
    data analytics courses
    360DigiTMG

    ReplyDelete
  31. Nice post. I learned some new information. Thanks for sharing.....A lot of opportunities are provided by the Sarkari Job, for which aspirants dream of. A huge number of vacancies are released We bring to you all the relevant notifications of government-jobs, which are updated 24 X 7....

    ReplyDelete
  32. Glad to chat your blog, I seem to be forward to more reliable articles and I think we all wish to thank so many good articles, blog to share with us.
    data analytics course
    data science course
    big data course in malaysia
    360DigiTMG

    ReplyDelete
  33. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
    data analytics course
    supply chain analytics beginner's guide
    big data course in malaysia
    360DigiTMG
    big data analytics training in malaysia

    ReplyDelete
  34. Two full thumbs up for this magneficent article of yours. I've really enjoyed reading this article today and I think this might be one of the best article that I've read yet. Please, keep this work going on in the same quality.
    data science bootcamp malaysia

    ReplyDelete
  35. I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job !
    Data Science Training in Bangalore

    ReplyDelete
  36. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. artificial intelligence ai and deep learning in coimbatore

    ReplyDelete
  37. I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got hereSalesforce Training in Chennai

    Salesforce Online Training in Chennai

    Salesforce Training in Bangalore

    Salesforce Training in Hyderabad

    Salesforce training in ameerpet

    Salesforce Training in Pune

    Salesforce Online Training

    Salesforce Training


    ReplyDelete
  38. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.data science courses

    ReplyDelete
  39. If your looking for Online Illinois license plate sticker renewals then you have need to come to the right place.We offer the fastest Illinois license plate sticker renewals in the state.
    Best Digital Marketing Courses in Hyderabad

    ReplyDelete
  40. Hi,

    Thank for sharing such a nice post on your blog keep it up and share more.

    Free Crack Software Download

    ReplyDelete
  41. I have a mission that I’m just now working on, and I have been at the look out for such information
    business analytics course

    ReplyDelete
  42. Very much impressive blog post,
    Digital Marketing Training in KPHB with 100% Internships & Job Assistance

    ReplyDelete
  43. I am sure that this is going to help a lot of individuals. Keep up the good work. It is highly convincing and I enjoyed going through the entire blog.

    business analytics course

    ReplyDelete
  44. I am overwhelmed by your post with such a beautiful subject. I usually visit their blogs and update myself through the information they contain, but today's blog would be most appreciated. Well done!
    Data Analytics Course in Bangalore

    ReplyDelete
  45. I am hoping the same best effort from you in the future as well. In fact your creative writing skills has inspired me.
    digital marketing courses in hyderabad with placement

    ReplyDelete
  46. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.

    DevOps Training in Hyderabad

    ReplyDelete
  47. Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing. python course in delhi

    ReplyDelete
  48. I don't have time to read your entire site right now, but I have bookmarked it and added your RSS feeds as well. I'll be back in a day or two. Thank you for this excellent site.
    Best Data Science Courses in Bangalore

    ReplyDelete
  49. This post is very simple to read and appreciate without leaving any details out. Great work!
    data science training

    ReplyDelete
  50. This is my first time visiting here. I found so much entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the good work. I have been meaning to write something like this on my website and you have given me an idea.
    data scientist training in hyderabad

    ReplyDelete

  51. i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
    data engineering course in india

    ReplyDelete
  52. You have completed certain reliable points there. I did some research on the subject and found that almost everyone will agree with your blog.
    Cloud Computing Certification in Bangalore

    ReplyDelete
  53. Thanks for sharing this informative blog. I have read your blog and I gathered some valuable information from this blog. Keep posting. Recently whatsapp mod

    ReplyDelete
  54. Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks! .
    <a href="https://360digitmg.com/india/data-analytics-certification-training-course-in-bangalore>Data Analytics training in Bangalore</a>

    ReplyDelete
  55. It's like you've got the point right, but forgot to include your readers. Maybe you should think about it from different angles.

    Best Ethical Hacking Institute in Bangalore

    ReplyDelete
  56. This comment has been removed by the author.

    ReplyDelete
  57. With a powerful tool is now easily available online like Water Jet Cutting Machine and Tile Cutting Machine from ChinaYongTAO across China.

    ReplyDelete
  58. Take new contracts & fly in realistic weather conditions with real-time traffic & earn money to expand your aircraft fleet. Flight Simulator Buy new airplane, City Pilot Flight Games A bigger plane. Choose new flying routes, improve your skills & get a new pilot license. Airplane Pilot Games.

    ReplyDelete
  59. I'm always looking online for articles that can help me. I think you also made some good comments on the functions. Keep up the good work!

    Data Scientist Course in Bangalore

    ReplyDelete
  60. It is extremely nice to see the greatest details presented in an easy and understanding manner.
    data science institutes in hyderabad

    ReplyDelete
  61. Get intelligent suggestions in the Editor Overview pane in Word and let Editor assist you across documents, email, and on the web MS Office 2016 Crack Key

    ReplyDelete
  62. Data Science is a growing domain with multitude of job opportunities, start your Data Scientsist Course today with 360DigiTMG and become a Data Scientist

    Data Science Course in Bangalore

    ReplyDelete
  63. Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts.
    data science training in malaysia

    ReplyDelete
  64. When two cards are dealt and the entire of the cards equals greater than nine, remove the first number of the entire. For example, if the Player’s hand is a 7 and a 6, the entire is thirteen, or 3. That’s end result of|as a outcome of} the banker’s hand has a 45.8% probability of profitable comparability with} the forty four.6% probability 파라오바카라 of the player’s hand.

    ReplyDelete
  65. Enterprise Risk Management Software (ERM) software, also known as Integrated Risk Management (IRM) software, is a comprehensive solution designed to assist organizations in managing and addressing risks across all levels of the enterprise. It provides a centralized platform for identifying, assessing, monitoring, and mitigating risks, enabling a holistic and integrated approach to risk management.

    ReplyDelete
  66. Our Marketing Automation Course training in Hyderabad provides an exceptional opportunity for all marketing enthusiasts wishing to climb the career ladder in fast-paced digital industries. This well-structured training integrates essential knowledge of the available marketing automation tools that will transform your perception of marketing strategies. It doesn't just instruct.
    Marketing Automation Course training in Hyderabad

    ReplyDelete