Thanks for the quick reply! Your suggestion works fine.  Perfect for my low
traffic server with lots of small sites.  I'll look into getting Apache to
manage redirects if (when!) it gets busier.

The redirect is useful as if a user comes to the www. version of the site
and then to the no www version they lose their session as the cookies don't
match.  With your suggestion the session is maintained regardless of the URL
entered.

On 2 February 2011 16:43, Jonathan Lundell <jlund...@pobox.com> wrote:

> On Feb 2, 2011, at 7:52 AM, Tom Atkins wrote:
> > Thanks for your work on the new routers Jonathan - it makes life much
> easier.
> >
> > Quick question, say I have:
> >
> > routers = dict(
> >   BASE  = dict(
> >       domains = {
> >           'domain1.com' : 'app1',
> >           'domain2.com' : 'app2',
> >       }
> >   ),
> > )
> >
> > But would also like www.domain1.com to map to app1 but also change the
> URL to domain1.com (stripping the www part) what is the best way to
> achieve that?
> >
> > Ideally I'd like it to work the same way as this .htaccess:
> >
> > RewriteEngine On
> > RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
> > RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
> >
> > This silently redirects to the non www domain and sends a 301 to any
> search engines to make it clear that the only site is at domain1.com(nothing 
> at
> www.domain1.com).
> >
>
> If possible, it's better to do the redirection through .htaccess. It's more
> efficient, since web2py never has to run.
>
> This should also work, if you don't really need the redirect, so it's also
> efficient:
>
> routers = dict(
>  BASE  = dict(
>      domains = {
>          'domain1.com' : 'app1',
>           'www.domain1.com' : 'app1',
>          'domain2.com' : 'app2',
>      }
>  ),
> )
>
> My impression is that search engines (Google anyway) are fairly smart about
> encountering the same content at related domains, and IIRC Google has a
> protocol for telling them which address is preferred.
>
>
> If you still want to do it through web2py, I think I'd do the rewrite as
> above (for both domain1 and www.domain1) and in models/0redirect.py (or at
> any rate, before any other application code runs), something like:
>
> if request.env.http_host == 'www.domain1.com':
>    redirect("http://domain1.com%s"; % request.env.request_uri, 301)
>
> or more generally:
>
> redirects = {
>    'www.domain1.com' : 'domain1.com',
>    ...
> }
>
> if request.env.http_host in redirects:
>    scheme = request.env.get('wsgi_url_scheme', 'http').lower()
>    uri = "%s://%s%s" % (scheme, redirects[request.env.http_host],
> request.env.request_uri)
>    redirect(uri, 301)
>
> None of that is tested, and you'd want to check it all, including
> appropriate escaping (if it's not done already). But you get the idea. It
> also doesn't take port overrides into consideration.
>
>

Reply via email to