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

Lets pretend we are moving "" to "". 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})
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.


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

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... here we stitch clothes as per your design and selection

mohd Sameer said... 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 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
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