Solved

301 redirect rails app

Posted on 2011-09-22
10
607 Views
Last Modified: 2012-05-12
i need to redirect http://example.com to http://www.example.com for SEO. rails 2.3.12 app
0
Comment
Question by:depassion
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36585259
You can do this in your application_controller and redirect there.

before_filter :ensure_domain

  def ensure_domain
   # check your request
   # and then redirect redirect_to "http://www.example.com"
  end
0
 
LVL 4

Expert Comment

by:kristinalim
ID: 36585266
You can handle redirection for these from the web server itself (Apache, Nginx, Lighttpd), this way the web server will no longer have to pass the non-www requests to your Rails application:

http://blogbuildingu.com/articles/www-redirect-right-way
0
 
LVL 8

Expert Comment

by:LocoTechCJ
ID: 36588220
This should definitely be handled at the web server level, not in rails.  Which web server are you using, and we can help you with the details.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:depassion
ID: 36588246
linux ubuntu server with nginx/passenger
0
 

Author Comment

by:depassion
ID: 36588251
can't i just add a htaccess file to the public directory like normal hosted server setups?
0
 

Author Comment

by:depassion
ID: 36588268
actually sorry, its apache/passenger
0
 
LVL 8

Accepted Solution

by:
LocoTechCJ earned 500 total points
ID: 36588338
<VirtualHost *:80>
    ServerName example.com
    RedirectMatch permanent ^/(.*) http://www.example.com/$1
</VirtualHost>

Open in new window


...try this in your apache conf file(usually in /etc/httpd/.  Should do the trick.
0
 

Author Comment

by:depassion
ID: 36589582
nope, i've added the above. see the httpd file and terminal output

httpd.conf
#NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#    
<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot /www/docs/dummy-host.example.com
#    ServerName dummy-host.example.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
     ServerName ndthub.com
     RedirectMatch permanent ^/(.*) http://www.ndthub.com/$1
</VirtualHost>

[root@ndthub conf]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: [Fri Sep 23 15:44:46 2011] [warn] NameVirtualHost ndthub.com:80 has no VirtualHosts
                                                           [  OK  ]
0
 
LVL 4

Expert Comment

by:kristinalim
ID: 36594463
To get rid of the warning, if you have no virtual host for ndthub.com:80:
# Something like:
<VirtualHost ndthub.com:80>
  # ... Your virtual host configuration
</VirtualHost>

Open in new window

you can go ahead and remove the:
NameVirtualHost ndthub.com:80

Open in new window


I assume you retained your virtual host for www.ndthub.com? Please check if you have a ServerAlias declaration for ndthub.com in your virtual host configuration for www.ndthub.com -- maybe the non-www requests are still being handled by your www.ndthub.com virtual host instead of the one you've just created purely for non-www redirection.

Let us know how it goes.
0
 
LVL 8

Expert Comment

by:LocoTechCJ
ID: 36599768
Also, if changes are not appearing to have any effect on the server, run an "httpd -V" to verify your server config file.  And don't forget you have to restart apache when you make changes.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question