skip to Main Content

I would like to configure apache so that if a request comes in for a url that matches a pattern and is of a certain browser it gets redirected to a specific page.

I know I can redirect to a page reading the user agent to get the browser with this:

RewriteCond "%{HTTP_USER_AGENT}"  ".*Trident.*"

If it is IE for example, and the url matches a specific pattern, I would like it to then be redirected to a custom html page.

This is the url pattern:

https://example.com/alt/var/b?a=[variable]:[numeric variable]:[someother variables I don't car eabout]

Examples:
https://example.com/alt/var/b?a=cat:200:someotherstuff
https://example.com/alt/var/b?a=cat:350:someotherstuff
https://example.com/alt/var/b?a=dog1:200:someotherstuff
https://example.com/alt/var/b?a=dog1:99:someotherstuff

I am only interested in the first two variables. The first variable is always going to be different. The second variable, which can only be a number will always be different as well. If the second variable though is equal to 99 and the browser is IE, I would like it to redirect to my custom html page. My issue is I have not been able to figure out how to write the rule for this? The https://example.com/alt/var/b?a= part will always be the same and will never change.

2

Answers


  1. Could you please try following, written as per your shown samples. Please make sure you clear your browser cache before testing these urls. In case you want to match any digit after a= then this will work.

    RewriteEngine ON
    RewriteCond %{HTTP_USER_AGENT} Trident|MSIE [NC]
    RewriteCond %{THE_REQUEST} s/alt/var/b?a=[w-]+:[0-9]+:[^s]*s [NC]
    RewriteRule ^(.*)$ http://%{HTTP_HOST}/your_custom_page [L]
    
    Login or Signup to reply.
  2. If the second variable though is equal to 99 and the browser is IE, I would like it to redirect to my custom html page

    You may use this redirect rule:

    RewriteEngine On
    
    RewriteCond %{HTTP_USER_AGENT} Trident|MSIE [NC]
    RewriteCond %{THE_REQUEST} /alt/var/b?a=[^:]+:99:[^s&]*sHTTP [NC]
    RewriteRule ^ /custom.html? [L,NE,R=301]
    
    • %{HTTP_USER_AGENT} Trident|MSIE: Will match user agents of IE
    • :99:: Will only redirect if 2nd variable is 99 as per requirements
    • ? at the end of the target will strip off previously existing query string.

    However please also read this about user agent detection

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