CBuehler
CBuehler

Reputation: 33

Create htaccess file for domain redirection (Django app deployed in Heroku)

Situation:

I've bought a specific domain, let's say 'example.ch' for my Django application which is deployed in Heroku. I also created an automated SSL certificate on Heroku (ACM). This certificate is valid for the WWW-subdomain, i.e. ACM Status says 'OK'. It fails for the non-WWW root domain, so I deleted the root domain entry on Heroku.

Issue:

Goal: I would like to type 'www.example.ch' as well as 'example.ch' in the browser to be always redirected to 'https://www.example.ch'.

Approach (so far):

My Host (swizzonic.ch) does not allow for 'Alias' records. 'A' records are possible but not supported by Heroku (https://help.heroku.com/NH44MODG/my-root-domain-isn-t-working-what-s-wrong). Swizzonic support told me to use a htaccess-file for redirection.

As I understand so far, I have to extend my middleware accordingly (?). Note that I use the common options for SSL redirection (SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https'), SECURE_SSL_REDIRECT = True, SESSION_COOKIE_SECURE = True, CSRF_COOKIE_SECURE = True).

How can I create a htaccess-file, where do I have to store it and how does the content look like?

Many thanks in advance!

Upvotes: 2

Views: 590

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133508

Could you please try following Rules in your .htaccess file. Please make sure your .htaccess file is working fine(to enable it you could go through its documentation). This will convert every non http request to https with/without www in domain name.

Please clear your browser cache before testing your URLs.

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

Upvotes: 2

Related Questions