Search code examples
amazon-web-serviceshttp-redirectmod-rewritehttpsamazon-cloudfront

What is the difference between these 2 snippets of code?


Both snippets are to redirect http to https but one works with AWS cloudfront distribution but the other don't. On its own, both works fine. What is the difference between these 2 snippets of code?

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^(localhost|127.0.0.1)
    RewriteRule ^/(.*) https://example.com/$1 [R,L] 
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Solution

  • Well, there are a couple of differences between the 2.

    The first block will Redirect to https://example.com/*

    If HTTPS is not ON AND if HTTP_HOST is neither localhost or 127.0.0.1

    The second block will redirect to the same host as the host used in the request using the https protocol.

    Eg:

    From: http://www.example.com/test?1=2&3=4

    To: https://www.example.com/test?1=2&3=4

    If HTTPS is not ON OR if HTTP_HOST is www.example.com (case incencitive).

    Since it will redirect to the same host as the one from the request, this would most likely create an infinite loop. The key difference being the [OR] flag specified in your condition.