Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

301 redirect rails app

Posted on 2011-09-22
10
Medium Priority
?
634 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month12 days, 14 hours left to enroll

971 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