Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does anyone know if it's possible to configure apache to modify the HTTP status code of a response after it has been generated?

Basically, I have a script running under multiple host names that redirects users to a dynamically determined location using a 302 status code, which is generated by the script itself (i.e. a PHP script which sets a 'Location' header). What I want to do is change the status code from 302 to 301 for just one of the host names, while keeping the rest using 302, and I want to do it at the server configuration level so that the script need not be modified.

I've had a look around the apache manual and found mod_headers, but it doesn't seem to be able to modify the status code of a response, only the headers that follow it. Can anyone tell me if there is there a way to do this, or am I stuck with my only option being to modify the script?

share|improve this question
up vote 0 down vote accepted

You can use mod_rewrite to accomplish this, i.e with the following example:

RewriteCond %{HTTP_HOST}
RewriteCond %{REQUEST_URI} !^/
RewriteRule ^(.*)$$1 [R=301,L]

Placing an .htaccess under the hostname you want to redirect will make this work. You can also modify your vhost configuration as well. Don't forget RewriteEngine On if not set already.

share|improve this answer
mod_rewrite is only able to act pre-response (i.e. before the script returns a response), isn't it? I need to modify the the status line post-response (i.e. after the script has returned a response). To my knowledge this is not possible with mod_rewrite - or am I wrong? – aweraw Apr 1 '14 at 2:58
I don't think this is possible at all least with Apache. Nginx has some config magic to make this work. – Nathan C Apr 1 '14 at 3:08
I was beginning to suspect this might be the case. Thanks for your input. – aweraw Apr 1 '14 at 3:17

Your Answer


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.