Categories
Programming

Apache Trick – Rewrite to 404 Not Found

This may be a well known trick, but I couldn’t find it anywhere. If you’re needing to hide certain files using .htaccess, the only way to do so according to the documentation is by using [G] for “410 Gone” or [F] for “403 Forbidden”. In my case, I didn’t want the user to know the file existed at all, so here’s the trick:


RewriteRule ^hidden - [R=404]

“^hidden” tells the server to look for any path on the server that starts with hidden. The “-” tells the server not to rewrite the URL, and the [R=404] tells the server to do a external redirect (making your browser go to the new location) to nowhere, and gives a 404 Not Found status.

Update: Apache 2.0 seems to only accept R=3xx, not R=404, so I think this must only work in 2.2 and up.