0

I'm trying to redirect http to https on an AWS server.

I tried the following htaccess syntax, nothing works:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]

And

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The website still loads with http, how can I fix that?

5
  • AllowOveride All - this is activated, yes? Commented Sep 7, 2017 at 9:00
  • @Lag I think this is his problem. Commented Sep 7, 2017 at 9:07
  • If you change RewriteCond %{HTTPS} off to RewriteCond %{HTTPS} !on do you get a redirect loop? Commented Sep 7, 2017 at 11:05
  • @Lag thank you, that was it :) Commented Sep 7, 2017 at 12:43
  • No problem - if in doubt blame configuration lol Commented Sep 7, 2017 at 13:07

1 Answer 1

2

This will 301 redirect all http links to https

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} Off [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]

If you want your site to also force www use this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} Off [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

Edit:

Mod_rewrite is disabled by default on AWS. You need to enable it in your httpd.conf file beneath the following line: /var/www/html change AllowOverride None to AllowOverride All and restart Apache.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, thank you for your answer, however, this still doesn't work for me, here is a screenshot prntscr.com/gi4rgm neither www to non www works nor the http to https.
There's probably a configuration problem on the AWS side. Have a look at this to enable mod_rewrite forums.aws.amazon.com/thread.jspa?messageID=265770
Make an edit to your answer @KeesSonnema about the configuration issue. Will make your answer more relevant then :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.