0

You know how Apache has many server variables like %{REQUEST_URI}, and %{ENV:REDIRECT_STATUS}

What is the variable name for the current request's HTTP status code?

I ask this because I want to write a RewriteCond that says 'if the HTTP status code is 302, then...'. But I cannot figure out what the server variable for this would be.

Your help would be greatly appreciated!

3
  • httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond Commented Aug 26, 2016 at 20:59
  • 5
    The code probably is NOT available, since it can't be determined until request processing is completed. e.g. there's no point in testing for a 200 code if you're doing "pretty" urls, because the pretty url almost NEVER exists as an actual file on the server. so by definition all pretty urls themselves are 404s. Only what they get rewritten TO would be a 200. Commented Aug 26, 2016 at 21:01
  • Thanks Mark, I appreciate the info! Commented Aug 29, 2016 at 12:59

1 Answer 1

2

There is no such thing as a request status code. Only the response, sent back from the server to the client, has an HTTP status code.

The closest you can do is setting a Custom Error Responses via an ErrorDocument directive. This means, based on some status code e.g. 404, you can call some server side script, which acts appropriately.

ErrorDocument 404 /var/www/html/errors/handle_404.php

This works for status codes starting with 400 and 500, but not for response codes in the range of 200 or 300. See again Custom Error Responses

Customized error responses can be defined for any HTTP status code designated as an error condition - that is, any 4xx or 5xx status.


Addressing comments, the syntax of ErrorDocument is defined as

Syntax: ErrorDocument error-code document

So there is just the error-code (4xx-5xx) to handle, and some document to show or execute, no way to define any additional flags.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Olaf. I appreciate your concise answer. The error document will not help me, but now I know I need to find another solution to my problem.
Maybe filters is another option, though I have no experience using those. You must look through the manual and see, if there's something appropriate.
Hey Olaf - thanks. Here is is what I want to achieve now: I have a php script and a apache rewriteRule that are creating an infinite loop... I want apache to only redirect to xwz.php IF xyz.php is not the one that just redirected to this current resource. I just don't know how to say that. Maybe you have some pointers on how this can be achieved?
ErrorDocument 404 /var/www/html/errors/handle_404.php [R=200, L] this the correct, if i want to pass 200 OK response code for all 404 pages?
@RajeevRanjanSharma If this is a question, ask a new question! Apart from that, the "Syntax" part of ErrorDocument is pretty clear: Syntax: ErrorDocument error-code document. So no status code or flags anywhere.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.