301 redirect rails app

i need to redirect http://example.com to http://www.example.com for SEO. rails 2.3.12 app
depassionAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
LocoTechCJConnect With a Mentor Commented:
<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
 
Kiran SonawaneProject LeadCommented:
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
 
kristinalimCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LocoTechCJCommented:
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
 
depassionAuthor Commented:
linux ubuntu server with nginx/passenger
0
 
depassionAuthor Commented:
can't i just add a htaccess file to the public directory like normal hosted server setups?
0
 
depassionAuthor Commented:
actually sorry, its apache/passenger
0
 
depassionAuthor Commented:
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
 
kristinalimCommented:
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
 
LocoTechCJCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.