skip to Main Content

I want to create an apache rule based on the user agent AND on the http response.

For example, I want a rule that says: if user-agent=test and the status of the request based on this user agent=503, redirect the request to /home

On the documentation page, I found a variable for based on the user agent(HTTP_USER_AGENT), but nothing for the http status.
Is that possible?

2

Answers


  1. The documentation page mentions the following environment variables:

    HTTP_ACCEPT
    HTTP_COOKIE
    HTTP_FORWARDED
    HTTP_HOST
    HTTP_PROXY_CONNECTION
    HTTP_REFERER
    HTTP_USER_AGENT
    AUTH_TYPE
    CONN_REMOTE_ADDR
    CONTEXT_PREFIX
    CONTEXT_DOCUMENT_ROOT
    IPV6
    PATH_INFO
    QUERY_STRING
    REMOTE_ADDR
    REMOTE_HOST
    REMOTE_IDENT
    REMOTE_PORT
    REMOTE_USER
    REQUEST_METHOD
    SCRIPT_FILENAME
    DOCUMENT_ROOT
    SCRIPT_GROUP
    SCRIPT_USER
    SERVER_ADDR
    SERVER_ADMIN
    SERVER_NAME
    SERVER_PORT
    SERVER_PROTOCOL
    SERVER_SOFTWARE
    TIME_YEAR
    TIME_MON
    TIME_DAY
    TIME_HOUR
    TIME_MIN
    TIME_SEC
    TIME_WDAY
    TIME
    API_VERSION
    CONN_REMOTE_ADDR
    HTTPS
    IS_SUBREQ
    REMOTE_ADDR
    REQUEST_FILENAME
    REQUEST_SCHEME
    REQUEST_URI
    THE_REQUEST
    

    Sadly, I am unable to find any variables that show the HTTP status.

    Login or Signup to reply.
  2. You could do something like the following, to define a custom 503 "error document" when a request with the User-Agent "test" is sent:

    <If "%{HTTP_USER_AGENT} == 'test'">
        ErrorDocument 503 https://example.com/home
    </If>
    

    When an absolute URL is used in the ErrorDocument directive, it will trigger a 302 (temporary) redirect to that URL should the stated HTTP response status be triggered by Apache.

    So, the above will issue an external redirect to /home when a request with User-Agent "test" triggers a 503 response in Apache.

    This will need to be defined in the main server config (not .htaccess) to stand a chance of catching an arbitrary 503 generated by the server. Although it is questionable whether this would catch such an "unexpected" error since a 503 – if triggered by the server itself – is a rather serious/fatal error. Ordinarily, a 503 is a controlled response for when the application goes into "maintenance mode". In this scenario, you would arguably handle this differently.

    Alternatively, the REDIRECT_STATUS environment variable holds the HTTP response status code so you could do something like the following using mod_rewrite to override the ErrorDocument and trigger a 302 redirect to /home:

    RewriteEngine On    
    
    RewriteCond %{ENV:REDIRECT_STATUS} =503
    RewriteCond %{HTTP_USER_AGENT} "=test"
    RewriteRule ^ /home [R=302,L]
    

    If the 503 is triggered by the application itself (eg. in PHP) then this will not be caught by the above rule(s).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search