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.

66 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

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

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

Calfre India said...

CALFRE handles oracle fusion financials online training and its modules maintaining classroom based training with the self-paced videos. An expert having ten plus years of self-experience handles the training period through online and explains each and every point perfectly. We recently launched our institute in the USA and getting
the best reputation over there.




Oracle fusion Financials Training in hyderabad

Oracle Fusion Financials online Training in hyderabad

Narendra Modi said...

RRB jobs
railway jobs

dokter suketi 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.
cara menggugurkan kandungan

keerthi dhivi said...

Thank you for such sharing such great information with us.
Online Marketing Services
Top SEO Company in Bangalore
seo plans india

mohd Sameer said...

https://stitchclothes.com here we stitch clothes as per your design and selection

mohd Sameer said...

https://stitchclothes.com here we stitch clothes as per your design and selection

blackkutty said...

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

impress bss said...

Thank you for sharing valuable information
Mobile app development company in chennai
Web design company in chennai
Web development company in chennai

ajay. chinna said...

Thanks for sharing the information, Salesforce experts a lot of openings in multi-level companies, for more information n
Salesforce Training
Salesforce Online Training in Bangalore
Salesforce Training online in India

raina chiyaan 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

selir selir said...

just information we only provide information for those who need it cara menggugurkan hamil
A. cara agar cepat hamil setelah selesai haid
B. cara agar cepat hamil
C. cara alami untuk segera mendapat kehamilan
D. makanan dan minuman agar cepat hamil
E. masa subur biar cepat hamil
F. panduan agar cepat hamil

Shiva Anand said...

We repair and service all models of Honor Service in Chennai, We rely on customer satisfaction. We can service all problems faced by Honor customers in Chennai.


Honor Service Center in Chennai | Honor Service Centre | Honor Service Center | Huawei Service Center in Chennai | Honor Service Center in velachery | Honor Service Chennai

Shiva Anand said...

we repair and service all models of oneplus Service in Chennai, We rely on customer satisfaction. We can service all problems faced by oneplus customers in chennai.


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

chirunag said...


tgjobsnews is India's best job websiteAll central Govt Jobs

chirunag said...


awesomestock is best shopping zone ethic wear dresses and Tops for women

Railway Results said...

Looking For RRB ALP Merit List? Look no more. Here, we are, providing the all RRB Merit List, may it be Group D, ALP, and RPF right, at one place at railwayresult.in. So, keep yourself updated with RRB Merit List.

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

Htc Service Center in Chennai 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

Tanika Co Valda said...

Great Article
Final Year Project Domains for CSE
Project Centers in Chennai



JavaScript Training in Chennai
JavaScript Training in Chennai

Excelanto Global Services said...

Really nice and Useful information shared. I am very happy to read this article.
ERP in India
Cloud ERP Companies in India
Web development company in chennai

Sanvi said...

Thank you so much for posting this. I really appreciate your work. Keep it up. Great work!

CEH Training In Hyderbad

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

OLIVIA H 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

Adhuntt said...

Nice article Instagram and Facebook have provided an amazing place for new brands to grow and flourish. We can find the perfect niche for your brand on the best social media platforms.

Kayal m said...

Nice article! It looks like a blog and I got different ideas from your great post. Keep it up and do well...!
Primavera Training in Chennai
Primavera Coaching in Chennai
Corporate Training in Chennai
Embedded System Course Chennai
Oracle Training in Chennai
Tableau Training in Chennai
Spark Training in Chennai
Excel Training in Chennai
Power BI Training 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 sbi
flipkart sbi debit card emi
flipkart sbi offer
flipkart sbi offers
flipkart offer with sbi card
flipkart sbi card offer
flipkart sbi credit card offers
flipkart sbi debit card offer
flipkart sbi debit card emi eligibility sms
sbi flipkart eligibility
sbi woohoo flipkart gift card
flipkart sbi debit card emi eligibility check
flipkart sbi offer big billion day
flipkart debit card emi sbi sms
flipkart emi with sbi debit card
flipkart sbi offer 2018
flipkart emi through sbi debit card
flipkart sbi emi debit card
flipkart sbi debit card emi eligibility check sms
flipkart sbi offer terms and conditions
flipkart sbi emi offer
flipkart debit card emi sbi mobile
flipkart mobile sbi card offer
flipkart sbi offer today
flipkart sbi card offer today
sbi po flipkart
flipkart coupons for sbi credit card
flipkart and sbi card offer
sbi flipkart voucher offer
flipkart sbi discount
flipkart and sbi
flipkart and sbi debit card offer
flipkart diwali sale sbi
flipkart diwali sale sbi offer
flipkart offer for sbi debit card
flipkart sbi 10 cashback offer
flipkart sbi 10 offer
flipkart sbi debit card emi 2018
flipkart sbi offer 2017
flipkart sbi offer big billion sale
flipkart sbi offer december 2017
flipkart sbi offer july 2018
flipkart sbi offer september 2017
flipkart sbi offer t&c
flipkart sbi terms and conditions
sbi flipkart big billion offer

flipkart offers said...




flipkart sbi
flipkart sbi debit card emi
flipkart sbi offer
flipkart sbi offers
flipkart offer with sbi card
flipkart sbi card offer
flipkart sbi credit card offers
flipkart sbi debit card offer
flipkart sbi debit card emi eligibility sms
sbi flipkart eligibility
sbi woohoo flipkart gift card
flipkart sbi debit card emi eligibility check
flipkart sbi offer big billion day
flipkart debit card emi sbi sms
flipkart emi with sbi debit card
flipkart sbi offer 2018
flipkart emi through sbi debit card
flipkart sbi emi debit card
flipkart sbi debit card emi eligibility check sms
flipkart sbi offer terms and conditions
flipkart sbi emi offer
flipkart debit card emi sbi mobile
flipkart mobile sbi card offer
flipkart sbi offer today
flipkart sbi card offer today
sbi po flipkart
flipkart coupons for sbi credit card
flipkart and sbi card offer
sbi flipkart voucher offer
flipkart sbi discount
flipkart and sbi
flipkart and sbi debit card offer
flipkart diwali sale sbi
flipkart diwali sale sbi offer
flipkart offer for sbi debit card
flipkart sbi 10 cashback offer
flipkart sbi 10 offer
flipkart sbi debit card emi 2018
flipkart sbi offer 2017
flipkart sbi offer big billion sale
flipkart sbi offer december 2017
flipkart sbi offer july 2018
flipkart sbi offer september 2017
flipkart sbi offer t&c
flipkart sbi terms and conditions
sbi flipkart big billion offer

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

Aravind.mylram 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"

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

Savanah Eden said...

We help you to get best websites and good ranking in search engines, visit us
Erp software development company in chennai
Professional webdesigning company in chennai
Best seo company in chennai
Crm software development company in chennai

swathima said...

thanks for your extrodinary services , go ahead
Scaffolding dealers in chennai

jose said...

Really nice post. Thank you for sharing amazing information.
Java Training in Chennai/Java Training in Chennai with Placements/Java Training in Velachery/Java Training in OMR/Java Training Institute in Chennai/Java Training Center in Chennai/Java Training in Chennai fees/Best Java Training in Chennai/Best Java Training in Chennai with Placements/Best Java Training Institute in Chennai/Best Java Training Institute near me/Best Java Training in Velachery/Best Java Training in OMR/Best Java Training in India/Best Online Java Training in India/Best Java Training with Placement in Chennai

venkatesh@CS said...

Excellent Blog. Thank you so much for sharing.
best react js training in chennai
react js training in Chennai
react js workshop in Chennai
react js courses in Chennai
react js training institute in Chennai
reactjs training Chennai
react js online training
react js online training india
react js course content
react js training courses
react js course syllabus
react js training
react js certification in chennai
best react js training

Tashi said...

professional bridal makeup artist in chennai Style Specializes in beauty bridal makeup and makes assured that individual bride should look like a princess.

best bridal makeup artist in chennai

Unknown said...

Excellent information with unique content and it is very useful to know about the information based on blogs.
Erp In Chennai
IT Infrastructure Services
ERP software company in India
Mobile Application Development Company in India
ERP in India
Web development company in chennai

jose said...

Really nice post. Thank you for sharing amazing information.
Java Training in Credo Systemz/Java Training in Chennai Credo Systemz/Java Training in Chennai/Java Training in Chennai with Placements/Java Training in Velachery/Java Training in OMR/Java Training Institute in Chennai/Java Training Center in Chennai/Java Training in Chennai fees/Best Java Training in Chennai/Best Java Training in Chennai with Placements/Best Java Training Institute in Chennai/Best Java Training Institute near me/Best Java Training in Velachery/Best Java Training in OMR/Best Java Training in India/Best Online Java Training in India/Best Java Training with Placement in Chennai

Venkatesh CS said...

Thanks for sharing valuable information.
Digital Marketing training Course in chennai
digital marketing training institute in chennai
digital marketing training in Chennai
digital marketing course in Chennai
digital marketing course training in omr
digital marketing certification in omr
digital marketing course training in velachery
digital marketing training center in chennai
digital marketing courses with placement in chennai
digital marketing certification in chennai
digital marketing institute in Chennai
digital marketing certification course in Chennai
digital marketing course training in Chennai
Digital Marketing course in Chennai with placement
digital marketing courses in chennai

william james said...

Looking for CDR Report Writing who can provide the best quality CDR report writing help service to engineers, visit at essaycorp.com.au We will help you to get A+ Grades in CDR report.
cdr report writing