I run a plain PHP site on shared hosting. No Node, no build step, no server I control. That means one small file does a lot of the security and routing work for me: .htaccess .
It sits in the root of the site, Apache reads it on every request, and two recipes in it are the ones I would never ship a site without. The first forces HTTPS. The second hides my config files from the web.
Forcing HTTPS
Shared hosting usually gives you a free certificate these days, but having one is not the same as using it. Until I added the redirect, the plain http:// version of my site still answered. Search engines could index both versions, and a visitor following an old link could end up on the unencrypted one.
The fix is three lines of mod_rewrite:
RewriteEngine On RewriteCond % { HTTPS } off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R = 301 ]
Enter fullscreen mode
Exit fullscreen mode
The condition checks whether the request came in over HTTPS.β¦