SaltyCrane Blog — Notes on JavaScript and web development

Card store project #5: Redirecting my www-prefixed domain to my non-www-prefixed domain

For search engine optimization and analytics, I wanted to make http://www.handsoncards.com/ permanently redirect to http://handsoncards.com/. So I started Googling. It took me a while to figure this one out... The first few articles I read suggested using Rewrite directives in my .htaccess file but this didn't work for me (maybe because I'm using mod_python and Django?). Then I found I could use Rewrite in my httpd.conf file, but I got an infinite redirect loop. Finally, I found the solution from this discussion. Here's what I did. I'm using Django, mod_python, and Apache on Ubuntu at Slicehost.

  • Enable mod_rewrite. Logged in as root:
    a2enmod rewrite
  • Edit /etc/apache2/httpd.conf:
    <VirtualHost *>
        ServerName handsoncards.com
        ServerAlias www.handsoncards.com
        #
        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^www\.handsoncards\.com
        RewriteRule (.*) http://handsoncards.com$1 [R=301,L] 
        #
        SetHandler python-program
        PythonHandler django.core.handlers.modpython
        SetEnv DJANGO_SETTINGS_MODULE handsoncards.settings
        PythonPath "['/srv/HandsOnCards', '/srv/python-packages'] + sys.path"
        PythonDebug Off
    </VirtualHost>
  • Restart Apache:
    /etc/init.d/apache2 restart

Comments