Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
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? –  Steve Madsen Jun 18 '12 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. –  Shane Madden Jun 19 '12 at 0:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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