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.

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

1 Answer 1

up vote 0 down vote accepted

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

RewriteCond %{HTTP_HOST} site2.com
RewriteCond %{REQUEST_URI} !^/site2.com
RewriteRule ^(.*)$ site2.com/$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
1  
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 at 2:58
    
I don't think this is possible at all actually...at least with Apache. Nginx has some config magic to make this work. –  Nathan C Apr 1 at 3:08
    
I was beginning to suspect this might be the case. Thanks for your input. –  aweraw Apr 1 at 3:17

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.