{"id":931,"date":"2018-12-31T15:32:45","date_gmt":"2018-12-31T23:32:45","guid":{"rendered":"https:\/\/www.goer.org\/?p=931"},"modified":"2018-12-31T15:32:45","modified_gmt":"2018-12-31T23:32:45","slug":"a-simple-htaccess-recipe-for-https-redirect-hsts","status":"publish","type":"post","link":"https:\/\/www.goer.org\/Journal\/2018\/12\/a-simple-htaccess-recipe-for-https-redirect-hsts.html","title":{"rendered":"A Simple .htaccess Recipe for HTTPS Redirect + HSTS"},"content":{"rendered":"<p>I like to get at least <em>one<\/em> blog post in a year &#8212; this one is coming in just under the wire!<\/p>\n<p>One of the phone screen questions I like to ask junior frontend candidates (and occasionally more experienced frontend candidates) is:<\/p>\n<blockquote>\n<p><em>Imagine you&#8217;re creating a website that doesn&#8217;t have any login capabilities, shopping carts, or anything other than static articles. Would you go to the effort of setting up HTTPS? Why or why not?&#8221;<\/em><\/p>\n<\/blockquote>\n<p>The answers I&#8217;m hoping to hear are along the lines of, &#8220;to make sure that people are actually connecting to your site,&#8221; or &#8220;to prevent a man-in-the-middle from tampering with your site.&#8221; Which then sometimes leads into an interesting discussion about TLS and what the candidate understands about networking.<\/p>\n<p>Important stuff! Which makes it at least a little bit embarrassing to admit that it was only this year that I got around to adding proper HTTPS with redirects to my own website. Now, at least, I can ask my interview question without feeling like a giant hypocrite.<\/p>\n<p>To make this post more about utility and less about self-flagellation, here&#8217;s the configuration I used. I&#8217;m on an old shared host, so this configuration is oriented towards people like myself, who are stuck hand-editing <code>.htaccess<\/code> files like a peasant. Interestingly, when you search for &#8220;redirect HTTP to HTTPS&#8221;, there aren&#8217;t actually that many pages that cover HTTPS redirects <em>and<\/em> HSTS in the same place, so perhaps this will be useful to somebody:<\/p>\n<pre><code>&lt;IfModule mod_rewrite.c&gt;\nRewriteEngine On\nRewriteCond %{SERVER_PORT} 80 \nRewriteRule ^(.*)$ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [R=301,L]\n&lt;\/IfModule&gt;\n\n&lt;IfModule mod_headers.c&gt;\nHeader set Strict-Transport-Security \"max-age=31536000; includeSubdomains;\"\n&lt;\/IfModule&gt;\n<\/code><\/pre>\n<p>Notes:<\/p>\n<ul>\n<li>If you&#8217;re <em>not<\/em> on a shared host and forced to use <code>.htaccess<\/code>, the Apache documentation <a href=\"http:\/\/httpd.apache.org\/docs\/current\/rewrite\/avoid.html#redirect\">recommends using <code>Redirect<\/code> instead <code>mod_rewrite<\/code><\/a>.<\/li>\n<li>The <code>Strict-Transport-Security<\/code> heading should be set only for HTTPS traffic. In this case, we&#8217;re in the clear because <code>Header set<\/code> applies to normal 2xx responses. If I had used <code>Header always set<\/code>, this would also set the heading on 3xx responses, which would be incorrect.<\/li>\n<li>I haven&#8217;t configured <a href=\"https:\/\/hstspreload.org\">HSTS preload<\/a> yet, but you might consider doing that.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Interestingly, when you search for &#8220;redirect HTTP to HTTPS&#8221;, there aren&#8217;t actually that many pages that cover HTTPS redirects *and* HSTS in the same place, so perhaps this will be useful to somebody:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-931","post","type-post","status-publish","format-standard","hentry","category-web"],"_links":{"self":[{"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/posts\/931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/comments?post=931"}],"version-history":[{"count":7,"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/posts\/931\/revisions"}],"predecessor-version":[{"id":938,"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/posts\/931\/revisions\/938"}],"wp:attachment":[{"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/media?parent=931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/categories?post=931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.goer.org\/Journal\/wp-json\/wp\/v2\/tags?post=931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}