Setting response status code without CGI
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.htmlRewriteEngine 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?
403 is the proper code for a web server to respond with if SSL is needed.
If I were you, I’d just set SSLRequireSSL in the port 80 vhost for that path, and let it do its thing.
Check more discussion of this question.
Related posts:
- status code 550- Mailbox unavailable.. server response was: 5.7.1 UNABLE TO RELAY on Edge Server 2010
- NGINX Reverse Proxy : Is it possible to proxy cache ONLY 404 (or other) html status codes?
- Apache logging to different files by response code
- What HTTP status code should an unconfigured domain return?
- Curl and Wget returns different response code
Leave a comment
Recent Posts
- What is the easiest way to upgrade my existing Perl 5.14 to Perl 5.16 on FreeBSD 9 using the ports system?
- Know if mysql has done its job
- Redirect https .com to https .co.uk without a valid SSL cert on .com without DNS change
- Why is it a bad idea to use customer email as from address
- 100% packets dropped on first RX queue on 3/5 raid6 iSCSI NAS devices using intel igb (resolved)





