Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a rails app served via Passenger (mod_rails) that I want to have a canonical URL.

I want all requests to go to http://mydomain.com (so, rewrite http://www.mydomain.com to http://mydomain.com).

On my other non-Rails apps, I accomplish this using mod_rewrite and an .htaccess file with the following:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.+)mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]

However, this doesn't seem to work with Passenger. I've tried all sorts of combinations of placing the .htaccess file (in the public directory), and moving the directives around (in the vhost configuration), but I can't get this to work.

How can I accomplish this?

share|improve this question
add comment

2 Answers

Try replacing that last line by the following:

RewriteRule ^(.*) http://mydomain.com%{REQUEST_URI} [R=permanent,QSA,L]

If that still fails, I recommend to use a separate virtual host for the URL www.mydomain.com that just does a Rewrite to mydomain.com. Passenger doesn't play well with mod_rewrite.

Hope this helps.

share|improve this answer
    
Your first suggestion didn't work, but your second one does. I used a Redirect instead of a Rewrite since I don't want that vhost (with www) hosting my app: RedirectMatch 301 ^(.*)$ http://mydomain.net$1 ). This works, but it is really inelegant and very un-Railslike. Is there really no better way?? –  neezer Jan 26 '10 at 6:30
    
I have always had problems with mod_rewrite and Passenger. Referring to your self-answer, check out the PassengerHighPerformance directive (modrails.com/documentation/…) relating to this. There is a suggested solution there too. Thanks for the details! –  Jonathan Clarke Jan 26 '10 at 10:38
    
Yeah, I saw that option too. However, the default for it (according to the docs) seems to be 'off', and my rewrite rules weren't working before, so at least for me that didn't work so well. But actually... now that I think about it... I just upgraded passenger last night, maybe I'll give it another shot... –  neezer Jan 26 '10 at 18:41
    
Yup, it was a version issue. I'm now running 2.2.9 and my original rewrite in the vhost works perfectly. I updated my post to reflect this, and include the older methods for older versions. Thanks for the help!! –  neezer Jan 26 '10 at 19:54
add comment
up vote 0 down vote accepted

UPDATE: Apparently I was running an older version of Passenger (don't know how I missed that). I'm currently running Passenger 2.2.9 and putting the original rewrite in the vhost works as expected (and desired)!

Google group discussion on workarounds for older versions of Passenger: http://groups.google.com/group/phusion-passenger/browse_thread/thread/5e019e5d65beb54f

For older versions:

Host the www as a separate vhost and redirect (Jonathan Clarke's suggestion)

RedirectMatch 301 ^(.*)$ http://mydomain.com$1

Do the rewrite in-app:

# before_filter :trim_www 
def trim_www 
  if request.subdomains.first == "www" 
    if request.subdomains == ["www"] 
      redirect_to "#{request.protocol}#{request. 
domain}#{request.port_string}#{request.path}" 
    else 
      subdomains = request.subdomains.shift.join(".") 
      subdomains << "." unless subdomains.blank? 
      redirect_to 
"#{request.protocol}#{subdomains}#{request.domain}#{request.port_string}#{r equest.path}" 
    end 
  end 
end 

(Props to RSL in the original post for this code)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.