What is the cleanest way to set a custom response code within Apache without resorting to CGI?
Twice in recent days I've wanted to do this. The first time I retired a web app. Status code 410 (gone) seemed the most appropriate. I came up with this snippet using mod_rewrite:
ErrorDocument 410 /retired.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !retired.html
RewriteRule . - [G]
retired.html
is a message displayed to anyone following links from elsewhere, so they understand what happened.
Now I have a web service API that should only work over HTTPS. For the HTML side, I have rewrite rules to redirect from HTTP to HTTPS, but for API access, I'd rather that unsecured requests get a hard error. (Some client HTTP libraries follow the redirect on GET, so it appears to work, then fail in weird ways on POST, PUT or DELETE.)
410 (gone) isn't right for this, 403 (forbidden) is closer, but still doesn't feel right. Obviously 3xx codes are completely wrong.
Is there no way, purely within Apache, to set the response status code for a request?