1

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?

1 Answer 1

1

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.

2
  • Didn't know about SSLRequireSSL. That's great for my current situation. Do you have any insight into my general question of setting arbitrary HTTP response status codes from within Apache? Jun 18, 2012 at 20:46
  • @SteveMadsen No - there's no mechanism for setting a status like 418 on a whim; Apache always wants to think there's a reason for responding the way that it is. Jun 19, 2012 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.