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.

83 comments:

Abdul bari said...

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

Unknown 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

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

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

Unknown 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

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

Unknown 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

Unknown said...
This comment has been removed by the author.
Unknown 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..

Unknown 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

Unknown said...

Jharkhand Labour Department Recruitment 2016

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

Anonymous said...

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

Unknown said...

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

Unknown said...

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


Digital Marketing Services in Chennai

Unknown said...

Good post. Thanks for sharing such a informative post.

PPC training in chennai

Praveen said...

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

Unknown said...

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

digital marketing training in chennai

Unknown 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

oraclefusion said...

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

Unknown said...

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.

blackkutty said...

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

Unknown said...

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


nancy said...

Very useful and informative.

SAP S/4 HANA Simple Finance Training in Hyderabad

Unknown said...

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

Unknown said...


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

chaitra691 said...


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

rohini said...

Nice sharing,
iphone display replacement in chennai | Mobile phone water damage service in chennai | iphone glass replacement in chennai | iphone battery replacement in chennai | 100% genuine apple parts | iphone unlocking service in chennai

flipkart offers said...



flipkart citibank offer
flipkart citibank offer 2018
flipkart citibank
flipkart citibank rewards points
flipkart citibank offer 2019
flipkart citibank card offer
flipkart citi bank
flipkart republic day citibank offer 2018
flipkart citi offer
flipkart citibank offer
flipkart citi credit card offers
flipkart citizen watches
flipkart citibank offer 2018
flipkart citibank
flipkart citibank offer august 2018
flipkart citi
flipkart citi
flipkart citibank rewards points
flipkart citi cash back offer
flipkart citibank cashback offer
flipkart citibank offer 2019
flipkart citibank card offer
citibank flipkart 10x
citibank flipkart voucher
flipkart citibank 10 cashback offer
flipkart citibank cash back
flipkart citibank offer 2017
flipkart citibank offer december 2017
flipkart citibank offer jan 2018
flipkart offers for citi credit card
citibank flipkart 2018
citibank flipkart gift card
flipkart citi cash back
flipkart citi offer 2018
flipkart citibank 10 cashback
flipkart citibank cashback 2018
flipkart citibank emi offer
flipkart citibank freedom sale
flipkart citibank offer freedom sale
flipkart citibank offer january 2018
flipkart citibank offer october 2017
flipkart citibank offer today
flipkart citibank republic day
flipkart republic day citibank offer 2018

flipkart offers said...



flipkart citibank offer
flipkart citibank offer 2018
flipkart citibank
flipkart citibank rewards points
flipkart citibank offer 2019
flipkart citibank card offer
flipkart citi bank
flipkart republic day citibank offer 2018
flipkart citi offer
flipkart citibank offer
flipkart citi credit card offers
flipkart citizen watches
flipkart citibank offer 2018
flipkart citibank
flipkart citibank offer august 2018
flipkart citi
flipkart citi
flipkart citibank rewards points
flipkart citi cash back offer
flipkart citibank cashback offer
flipkart citibank offer 2019
flipkart citibank card offer
citibank flipkart 10x
citibank flipkart voucher
flipkart citibank 10 cashback offer
flipkart citibank cash back
flipkart citibank offer 2017
flipkart citibank offer december 2017
flipkart citibank offer jan 2018
flipkart offers for citi credit card
citibank flipkart 2018
citibank flipkart gift card
flipkart citi cash back
flipkart citi offer 2018
flipkart citibank 10 cashback
flipkart citibank cashback 2018
flipkart citibank emi offer
flipkart citibank freedom sale
flipkart citibank offer freedom sale
flipkart citibank offer january 2018
flipkart citibank offer october 2017
flipkart citibank offer today
flipkart citibank republic day
flipkart republic day citibank offer 2018

Admin said...

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"

Admin said...

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

Priyanka said...

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

anusha said...

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

tejaswini said...

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

Anurag Srivastava said...

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

dataanalyticscourse said...

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

Anonymous said...

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

tejaswini said...

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

DataScience Specialist said...

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

360digitmgas said...

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

shankarjaya said...

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


EXCELR said...

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

360digitmg said...

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

Amlida James said...

Hi,

Thank for sharing such a nice post on your blog.

Best Home Network Firewall Device

Home Improvement said...

Hi,

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

Free Crack Software Download

Home Improvement said...

Excellent post tn text books

360digiTMG Training said...

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

Home Improvement said...

Thank you for sharing the post. godaddy coupon code

Rick said...

Thanks for sharing your thoughts.

Rick Anderson

Anonymous said...

Very informative. Thanks for sharing.

Best Bike Taxi Service in Hyderabad

Best Software Service in Hyderabad

Electric Vehicle Charging Stations

Anonymous said...

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

traininginstitute said...

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

Data Analytics Course in Bangalore said...

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

traininginstitute said...

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

lionelmessi said...

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

Data Scientist Course in Dombivli said...

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

Best Data Science Courses said...

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

training institute said...

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

dataanalytics said...

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

Pallavi reddy said...


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

Unknown said...

I am truly getting a charge out of perusing your elegantly composed articles. It would seem that you burn through a ton of energy and time on your blog. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome.business analytics course in gurgaon

Educational Courses said...

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

Arnold DK said...

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

DataCharge said...

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>

Professional Courses and Training said...

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

Pallavireddy said...
This comment has been removed by the author.
deviotect said...

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

Henry Lucas said...

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.

Professional Learning Institute said...

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

Unknown said...

This post is good enough to make somebody understand this amazing thing, and I’m sure everyone will appreciate this interesting things. business analytics course in surat

Unknown said...

Hey There. I found your blog using msn. This is a very well written article. I’ll be sure to bookmark it and come back to read more of your useful info. Thanks for the post. I’ll definitely return. business analytics course in kanpur

360DigiTMG said...

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

Unknown said...

This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest post. I will visit your blog regularly for Some latest post. data science training in surat

Unknown said...

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well.. data analytics course in mysore

Unknown said...
This comment has been removed by the author.
Cyberz Pc said...

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

Professional Academic Institute said...

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

traininginstitute said...

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

pallmauhles said...

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.

Alia parker said...

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.

lettie a. Wilson said...

Try to Be Specific in Your Comments.

350z Problems

3zenx said...

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