Solved

301 redirect rails app

Posted on 2011-09-22
10
621 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

718 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