I've got a Django powered website, and I am attempting to solve the "canonical hostname" problem. I would like www.example.com to redirect to example.com. I've attempted both techniques based in the Apache documentation here (scroll lower to Canonical hostnames).

I am presently using the mod_rewrite method, and that i have this inside a virtual host container:

   RewriteEngine on
   RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
   RewriteCond %{HTTP_HOST} !^$
   RewriteRule ^/?(.*)$ http://example.com/$1 [L,R=301,NE]

This works best for me, aside from one situation. In Opera only, basically type www.example.com inside a browser, it redirects and that i check this out within the URL bar: example.com// (note the two trailing slashes).

However, something similar to this works properly: www.example.com/news/ will get rerouted to example.com/news/.

I only check this out around the root URL in Opera. It appears to operate fine on Home windows under Chrome, IE9, and Opera (maybe individuals browsers consume the double slash?). My Mac using friend states it's fine in Safari, but younger crowd sees the issue in Opera.

So far as Django configurations go, I'm while using default worth of APPEND_SLASH=True. I'm not sure if Django has anything related to it, but I have attempted mod_rewrite rules such as the above on static HTML sites before also it always appears to operate.

Attempt to disregard the "/" by putting it into parenthesis, so, wheter it's here or otherwise, the final part should work (which is not $1 any longer but $2 within the redirection URL):

RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(/?)(.*)$ http://example.com/$2 [L,R=301,NE]

Please let me know whether it works.