PythonAnywhere: Redirect from non-www to www

Posted: October 16, 2024 | Updated: October 16, 2024

If you are using PythonAnywhere to host your Django application, you may encounter this issue: you can't redirect your domain with HTTPS from the naked version to the www version (e.g., from https://yourdomain.com/ to https://www.yourdomain.com/). This issue was recorded in my Google Search Console. Whenever I visit the naked version of the URL, I always get a 404 Not Found error, but the error page is not served from my Django application.

Example: Can't redirect URL in PythonAnywhere

  1. https://yourdomain.com/
  2. https://www.yourdomain.com/

I read the PythonAnywhere documentation, which states that GoDaddy only supports HTTP version redirects (see: Pythonanywhere Naked Domains). I am already using Cloudflare for SSL on my WordPress site. Cloudflare has an option to set page rules to redirect HTTP to HTTPS and from the non-www version to the www version. I implemented this, and it solved my problem.

Step 1: Add your site to CloudFlare

You can easily integrate your PythonAnywhere site with Cloudflare.

First, download your site's DNS records from your domain name provider (mine is GoDaddy). Then, add a new site (free version) in Cloudflare and import the DNS file to your Cloudflare account.

Finally, add the Cloudflare name servers to your domain's nameservers.

Step 2: Enable Free SSL certificate for HTTPS

  1. Go to the Cloudflare website and log in to your account.

  2. Click on the domain for which you want to enable SSL.

  3. In the Cloudflare dashboard, click on the SSL/TLS tab in the menu.

cloudflare free ssl

4. For the best security, choose Full (strict) if you have an SSL certificate on your server. If not, choose Flexible temporarily.

Step 3: Set Page Rules in Cloudflare to Redirect URLs

To set up a redirect from the non-www version to the www version of your domain using Cloudflare's Page Rules, follow these steps:

  1. Go to the Cloudflare website and log in to your account.

  2. Click on the domain for which you want to set up the redirect.

  3. Go to Page Rules:

    • In the Cloudflare dashboard, navigate to the Page Rules tab.cloudflare page rules
  4. Click on Create Page Rule.

  5. Set Up the Rule:

  • URL Pattern: Enter http://yourdomain.com/* (replace yourdomain.com with your actual domain).
  • Settings: Choose Forwarding URL and then select 301 - Permanent Redirect.
  • Destination URL: Enter https://www.yourdomain.com/$1 (again, replace with your actual domain). The $1 captures any additional path and query string.
  1. Add a Second Rule for HTTPS:

    • You should create another rule for the HTTPS version:
      • URL Pattern: Enter https://yourdomain.com/*
      • Settings: Choose Forwarding URL and then select 301 - Permanent Redirect.
      • Destination URL: Enter https://www.yourdomain.com/$1.
  2. Click Save and Deploy for each rule you created.

  3. Test the Redirects:

    • After a few minutes, test both http://yourdomain.com and https://yourdomain.com in your browser to ensure they redirect to https://www.yourdomain.com.

Notes

  • Propagation Time: Changes may take a few minutes to propagate, so be patient if the redirects don’t work immediately.
  • Order of Rules: Cloudflare processes page rules in the order they appear, so ensure your non-www rules are positioned correctly if you have multiple rules.

Step 4: Do this setting in your PythonAnywhere account and Django app

Turn off  force HTTPS redirect in  PythonAnywhere's "web apps" section

Configure Django Settings

In your Django settings, ensure that you have the following:

SECURE_SSL_REDIRECT = True  # Redirect all HTTP to HTTPS
SECURE_HOSTS = ['yourdomain.com', 'www.yourdomain.com']  # Add your domains

Ensure that your ALLOWED_HOSTS setting in your settings.py file includes both versions of your domain. It should look something like this:

ALLOWED_HOSTS = ['yourdomain.com', 'www.yourdomain.com']

That's all.

© 2024 Webapptiv. All rights reserved.