How can I detect if page was requested via a redirect?

后端 未结 6 759
北海茫月
北海茫月 2021-01-06 05:00

On a client\'s website there are loads of redirects going to a particular page. This page somehow needs to have a way detecting whether the request was direct (URI typed in

6条回答
  •  滥情空心
    2021-01-06 05:51

    This is what I recently did to detect a redirect and expose it as a custom request header to the application: X-Redirect: 1 (using Apache's mod_rewrite and mod_headers).

    On the source side, I redirect all requests to the target server by prepending an additional /redirect to the URL path:

    RewriteRule ^/(.*) http://target-server.com/redirect/$1 [L,R=permanent]
    

    On the target side, I detect the /redirect in the path, strip it via another redirect, and inject a custom cookie into the response:

    RewriteRule ^/redirect/(.*)$ /$1 [R=permanent,L,CO=redirect:1:target-server.com:86400:/]
    

    Also on the target side, I convert the cookie into an environment variable, "disable" the cookie, then convert the env variable into a custom request header:

    RewriteCond %{HTTP_COOKIE} redirect=1 [NC]
    RewriteRule ^(.*)$ /$1 [L,CO=redirect:0:target-server.com:86400:/,E=redirect:1]
    RequestHeader set X-Redirect 1 env=redirect
    

提交回复
热议问题