Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 (so, rewrite to

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 ^(.*)$$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

Try replacing that last line by the following:

RewriteRule ^(.*){REQUEST_URI} [R=permanent,QSA,L]

If that still fails, I recommend to use a separate virtual host for the URL that just does a Rewrite to 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 ^(.*)$$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 (…) 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
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:

For older versions:

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

RedirectMatch 301 ^(.*)$$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. 
      subdomains = request.subdomains.shift.join(".") 
      subdomains << "." unless subdomains.blank? 
"#{request.protocol}#{subdomains}#{request.domain}#{request.port_string}#{r equest.path}" 

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

share|improve this answer

Your Answer


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.