Menu

Security headers from .htaccess: the three lines I add to every PHP site
πŸ“°

Security headers from .htaccess: the three lines I add to every PHP site

DEV Community: phpΒ·Mobeen AhmedΒ·about 18 hours ago
#c6sPWlW9
#devto#dev#header#origin#browser
Long article (629 words) β€” no AI summary yet.
Reading 0:00
15s threshold

My site is plain PHP on shared hosting. No framework, no middleware, and no server config I can touch beyond one file: .htaccess . That sounds limiting, but the cheapest security hardening I do on any PHP site lives in three lines of that file.

The trick is mod_headers , which most shared hosts, including mine, have switched on already. It lets me attach response headers from .htaccess , and the browser does the rest. If your host has it off, these lines silently do nothing β€” which is why I check with curl -I after every deploy instead of assuming.

1. X-Content-Type-Options: nosniff

Header set X - Content - Type - Options "nosniff"

Enter fullscreen mode

Exit fullscreen mode

Browsers like to guess. If a file arrives with a vague or wrong content type, the browser sniffs the bytes and decides for itself what it is looking at. That guessing is how a file that should be treated as inert data gets executed as something else.

My site proxies video files, so this one matters more than most.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.