Adding (or removing) "www" without mod_rewrite

67 views
Skip to first unread message

Peter Cooper

unread,
Aug 7, 2008, 2:39:46 AM8/7/08
to Phusion Passenger Discussions
How do other Passenger users deal with removing or adding "www" to
their URLs without using mod_rewrite?

For example, if I have a site http://www.example.com/, I might want
all visitors to http://example.com/ to redirect to http://www.example.com/
(or vice versa). This is recommended for SEO purposes to help prevent
duplicate content being detected. Normally you'd use a rewrite rule
with a host-based condition to deal with this, but is there a way of
doing it without activating mod_rewrite?

I could always activate mod_rewrite and deal with it the classic way,
but since it's hinted that this isn't a Good Thing (tm) to do, I'd
like to avoid doing that.

Peter Cooper

unread,
Aug 7, 2008, 6:02:57 AM8/7/08
to Phusion Passenger Discussions
On Aug 7, 7:39 am, Peter Cooper <pcoo...@gmail.com> wrote:
> How do other Passenger users deal with removing or adding "www" to
> their URLs without using mod_rewrite?
>
> For example, if I have a sitehttp://www.example.com/, I might want
> all visitors tohttp://example.com/to redirect tohttp://www.example.com/
> (or vice versa). This is recommended for SEO purposes to help prevent
> duplicate content being detected. Normally you'd use a rewrite rule
> with a host-based condition to deal with this, but is there a way of
> doing it without activating mod_rewrite?
>
> I could always activate mod_rewrite and deal with it the classic way,
> but since it's hinted that this isn't a Good Thing (tm) to do, I'd
> like to avoid doing that.

Just to add in case it's useful for someone else, a few people have
suggested on Twitter having two virtual hosts, one for www and one
without, and then using a simple Redirect on one. That's not
acceptable in my case as I'm using cPanel and don't want to mess with
the VirtualHosts if I can avoid it (in which case mod_rewrite
reactivation is a better option).

Pete

Andrew Stone

unread,
Aug 7, 2008, 7:35:45 AM8/7/08
to phusion-...@googlegroups.com

Hey Peter,

Couldn't you use a before_filter on the application controller to test the request?  I do something similar for an ssl requiremet for specific controllers like:

    def redirect_to_ssl
      return if request.ssl?
     
      unless (request.ssl? or RAILS_ENV == 'development')
          redirect_to(url_for(params.merge({:protocol => 'https://'})))
      end
   
    end
 
Of course the before_filter is redirect_to_ssl.

Good luck,
andy


--
Andrew Stone

RSL

unread,
Aug 7, 2008, 8:19:54 AM8/7/08
to phusion-...@googlegroups.com
Yes. You can indeed redirect based on if request.subdomains.first ==
"www". I've had to do so in the past when I couldn't change the Apache
confs. It will cost an extra Rails request though, so if you can solve
it by rewriting the configurations, that's probably preferable.

RSL

Andrew Stone

unread,
Aug 7, 2008, 8:37:54 AM8/7/08
to phusion-...@googlegroups.com
On Thu, Aug 7, 2008 at 8:19 AM, RSL <sco...@gmail.com> wrote:

Yes. You can indeed redirect based on if request.subdomains.first ==
"www". I've had to do so in the past when I couldn't change the Apache
confs. It will cost an extra Rails request though, so if you can solve
it by rewriting the configurations, that's probably preferable.

RSL


It would only cost you an extra request one time though.  After it is set, you should be good.   You will have to test each request or store something in the session to flag that the test has been done.  I'm guessing storing a true/false value in the session to denote whether the test has been done will be faster than testing the actual request.subdomains for "www" every time.
 

--
Andrew Stone

secobarbital

unread,
Aug 10, 2008, 1:44:39 AM8/10/08
to Phusion Passenger Discussions
On Aug 6, 11:39 pm, Peter Cooper <pcoo...@gmail.com> wrote:
> I could always activate mod_rewrite and deal with it the classic way,
> but since it's hinted that this isn't a Good Thing (tm) to do, I'd
> like to avoid doing that.

Isn't mod_rewrite overridden because of the default .htaccess that is
generated by Rails?

http://www.modrails.com/documentation/Users%20guide.html#conflicting_apache_modules
"If you really want to use mod_rewrite on Rails virtual hosts, then
please set the RailsAllowModRewrite configuration option. But please
note that you will have to delete Rails applications'
default .htaccess file, or add rewrite rules to negate its effects."

Peter Cooper

unread,
Aug 10, 2008, 7:40:06 AM8/10/08
to Phusion Passenger Discussions


On Aug 10, 6:44 am, secobarbital <seggy.um...@gmail.com> wrote:
> On Aug 6, 11:39 pm, Peter Cooper <pcoo...@gmail.com> wrote:
>
> > I could always activate mod_rewrite and deal with it the classic way,
> > but since it's hinted that this isn't a Good Thing (tm) to do, I'd
> > like to avoid doing that.
>
> Isn't mod_rewrite overridden because of the default .htaccess that is
> generated by Rails?

I believe so, but there was a discussion here recently asking whether
that's the only reason why and, if not, what the other problem cases
were. I don't recall seeing an answer to that, so there might be edge
cases that could be thrown up by reactivating it. It's certainly worth
a try though.

Many thanks to Andrew and RSL, I hadn't thought of doing it at the app
level but that makes sense - thanks!

Pete

Scotttt

unread,
Sep 5, 2008, 3:42:11 AM9/5/08
to Phusion Passenger Discussions
> I believe so, but there was a discussion here recently asking whether
> that's the only reason why and, if not, what the other problem cases
> were. I don't recall seeing an answer to that, so there might be edge
> cases that could be thrown up by reactivating it. It's certainly worth
> a try though.
>
> Many thanks to Andrew and RSL, I hadn't thought of doing it at the app
> level but that makes sense - thanks!
>
> Pete

Hi Pete -

Have you written any code for this yet? Would you share if you have?
I'm looking at the same issue and was going to put something together
tomorrow, but if you've already solved it, so much the better. I think
this may become a popular thread over time. :)

RSL

unread,
Sep 5, 2008, 10:03:03 AM9/5/08
to phusion-...@googlegroups.com
Here's what I'm using:

# 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}#{request.path}"
    end
  end
end

RSL

Scotttt

unread,
Sep 5, 2008, 5:27:41 PM9/5/08
to Phusion Passenger Discussions
This worked great. Thanks!
Reply all
Reply to author
Forward
0 new messages