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 manually) or a redirect.
在客户的网站上有大量重定向到特定页面。此页面需要以某种方式检测请求是直接的(手动输入的URI)还是重定向。
-
All of the redirects are 301 redirects. Due to SEO rules added indicators need to be avoided. (Google indexes urls with values separately)
所有重定向都是301重定向。由于SEO规则,需要避免添加指标。 (Google使用值分别对URL进行索引)
-
I have tried checking all environment variables but all of them are empty (which assume is normal) and in this respect internal redirects are not any different (I reckon).
我已经尝试检查所有环境变量,但它们都是空的(假设是正常的),在这方面内部重定向没有任何不同(我估计)。
-
Detection needs to happen real time so log files are not an option.
检测需要实时发生,因此日志文件不是一个选项。
The process in short, parameters are registered in cookies via a script, then it 301 redirects to the page where the content is cookie driven. While cookies are registered the content won't change back to original when someone retypes the address in the address bar.
简而言之,参数通过脚本在cookie中注册,然后301重定向到内容由cookie驱动的页面。当cookie被注册时,当有人重新填写地址栏中的地址时,内容将不会更改回原始内容。
I hope this makes sense.
我希望这是有道理的。
PREVIOUSLY: I was thinking about status codes, but I am not sure if there is a way to read that on the destination page or not. (We've clearified that it won't work)
以前:我在考虑状态代码,但我不确定是否有办法在目标页面上读取它。 (我们已经澄清它不会起作用)
6 个解决方案
#1
Depending on the browser and intermediate proxies, several things can happen. However, in most cases you can't depend on any one thing. The status codes are something the server sends back to the browser, so you won't get those in a request.
根据浏览器和中间代理,可能会发生一些事情。但是,在大多数情况下,你不能依赖任何一件事。状态代码是服务器发送回浏览器的内容,因此您不会在请求中获取这些代码。
You don't say what problem you are trying to solve or why this bit is important. What problem are you trying to solve?
你没有说你想要解决什么问题或者为什么这一点很重要。你想解决什么问题?
Instead of relying on something you don't control, turn it into something you can control.
而不是依靠你无法控制的东西,把它变成你可以控制的东西。
-
Ensure that you're doing the right sort of redirect. There are redirections for permanent and temporary moves as well as other situation.
确保您正在进行正确的重定向。永久和临时移动以及其他情况都有重定向。
-
If you don't need the data in real time, you can figure out from log files. This is handy if you're trying to figure out traffic patterns, but not so useful in real time.
如果您不需要实时数据,可以从日志文件中找出答案。如果您试图找出流量模式,但实时不那么有用,这很方便。
-
Instead of an external redirection, make it an internal redirection. You can track it through your web server's request cycle.
而不是外部重定向,使其成为内部重定向。您可以通过Web服务器的请求周期跟踪它。
-
Set a cookie on the external redirection then look for it in the next request. This won't catch the people who don't set cookies though.
在外部重定向上设置cookie,然后在下一个请求中查找它。这不会抓住那些没有设置cookie的人。
-
Add path info to the redirection, or maybe query parameters as Sinan suggested.
将路径信息添加到重定向,或者可以像Sinan建议的那样查询参数。
#2
How about:
my $cgi = CGI->new;
print $cgi->redirect(
'http://example.com/this/page/that.html?redirect=yes'
);
What ever mechanism you are using to distinguish between redirects can then look at the query string to decide. Of course, this does not prevent users from bookmarking the URL with the redirect=yes
etc.
您使用什么机制来区分重定向,然后可以查看要查询的查询字符串。当然,这并不妨碍用户使用redirect = yes等为URL添加书签。
You could try the referer
header, but that has its own problems.
您可以尝试引用标头,但这有其自身的问题。
#3
I don’t think there’s a way to do that. The only indicator I can imagine is the Referer
header field. But it seems that it’s only sent if the request was initiated on a non-HTTP way (clicking a link, form submission, meta refresh, etc.).
我认为没有办法做到这一点。我能想象的唯一指标是Referer头字段。但似乎仅在请求是以非HTTP方式启动时才会发送(单击链接,表单提交,元刷新等)。
#4
Inspired by Brian I toke a step back and looked at what exactly is causing my problem.
灵感来自Brian,我退后一步,看看究竟是什么导致了我的问题。
There may not be an answer to this question but there is a solution that partially solves problem. By using session cookies the modified content will only exist for that particular session so next time the original content becomes accessible again. This doesn't change the fact that re-enterying the url in the addressbar will still result in the page using the cookie.
这个问题可能没有答案,但有一个解决方案可以部分解决问题。通过使用会话cookie,修改后的内容将仅存在于该特定会话,以便下次再次访问原始内容时。这并不会改变以下事实:重新输入地址栏中的网址仍然会导致页面使用cookie。
Thank you all for trying to help.
谢谢大家的帮助。
#5
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).
这是我最近检测重定向并将其作为自定义请求标头暴露给应用程序的方法:X-Redirect:1(使用Apache的mod_rewrite和mod_headers)。
On the source side, I redirect all requests to the target server by prepending an additional /redirect
to the URL path:
在源端,我通过在URL路径上添加额外/重定向来将所有请求重定向到目标服务器:
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:
在目标端,我检测路径中的/ redirect,通过另一个重定向将其剥离,并将自定义cookie注入响应中:
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:
同样在目标端,我将cookie转换为环境变量,“禁用”cookie,然后将env变量转换为自定义请求标头:
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
#6
If the request was a redirect, there should be a header indicating where it was redirected from.
如果请求是重定向,则应该有一个标题指示重定向的位置。
#1
Depending on the browser and intermediate proxies, several things can happen. However, in most cases you can't depend on any one thing. The status codes are something the server sends back to the browser, so you won't get those in a request.
根据浏览器和中间代理,可能会发生一些事情。但是,在大多数情况下,你不能依赖任何一件事。状态代码是服务器发送回浏览器的内容,因此您不会在请求中获取这些代码。
You don't say what problem you are trying to solve or why this bit is important. What problem are you trying to solve?
你没有说你想要解决什么问题或者为什么这一点很重要。你想解决什么问题?
Instead of relying on something you don't control, turn it into something you can control.
而不是依靠你无法控制的东西,把它变成你可以控制的东西。
-
Ensure that you're doing the right sort of redirect. There are redirections for permanent and temporary moves as well as other situation.
确保您正在进行正确的重定向。永久和临时移动以及其他情况都有重定向。
-
If you don't need the data in real time, you can figure out from log files. This is handy if you're trying to figure out traffic patterns, but not so useful in real time.
如果您不需要实时数据,可以从日志文件中找出答案。如果您试图找出流量模式,但实时不那么有用,这很方便。
-
Instead of an external redirection, make it an internal redirection. You can track it through your web server's request cycle.
而不是外部重定向,使其成为内部重定向。您可以通过Web服务器的请求周期跟踪它。
-
Set a cookie on the external redirection then look for it in the next request. This won't catch the people who don't set cookies though.
在外部重定向上设置cookie,然后在下一个请求中查找它。这不会抓住那些没有设置cookie的人。
-
Add path info to the redirection, or maybe query parameters as Sinan suggested.
将路径信息添加到重定向,或者可以像Sinan建议的那样查询参数。
#2
How about:
my $cgi = CGI->new;
print $cgi->redirect(
'http://example.com/this/page/that.html?redirect=yes'
);
What ever mechanism you are using to distinguish between redirects can then look at the query string to decide. Of course, this does not prevent users from bookmarking the URL with the redirect=yes
etc.
您使用什么机制来区分重定向,然后可以查看要查询的查询字符串。当然,这并不妨碍用户使用redirect = yes等为URL添加书签。
You could try the referer
header, but that has its own problems.
您可以尝试引用标头,但这有其自身的问题。
#3
I don’t think there’s a way to do that. The only indicator I can imagine is the Referer
header field. But it seems that it’s only sent if the request was initiated on a non-HTTP way (clicking a link, form submission, meta refresh, etc.).
我认为没有办法做到这一点。我能想象的唯一指标是Referer头字段。但似乎仅在请求是以非HTTP方式启动时才会发送(单击链接,表单提交,元刷新等)。
#4
Inspired by Brian I toke a step back and looked at what exactly is causing my problem.
灵感来自Brian,我退后一步,看看究竟是什么导致了我的问题。
There may not be an answer to this question but there is a solution that partially solves problem. By using session cookies the modified content will only exist for that particular session so next time the original content becomes accessible again. This doesn't change the fact that re-enterying the url in the addressbar will still result in the page using the cookie.
这个问题可能没有答案,但有一个解决方案可以部分解决问题。通过使用会话cookie,修改后的内容将仅存在于该特定会话,以便下次再次访问原始内容时。这并不会改变以下事实:重新输入地址栏中的网址仍然会导致页面使用cookie。
Thank you all for trying to help.
谢谢大家的帮助。
#5
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).
这是我最近检测重定向并将其作为自定义请求标头暴露给应用程序的方法:X-Redirect:1(使用Apache的mod_rewrite和mod_headers)。
On the source side, I redirect all requests to the target server by prepending an additional /redirect
to the URL path:
在源端,我通过在URL路径上添加额外/重定向来将所有请求重定向到目标服务器:
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:
在目标端,我检测路径中的/ redirect,通过另一个重定向将其剥离,并将自定义cookie注入响应中:
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:
同样在目标端,我将cookie转换为环境变量,“禁用”cookie,然后将env变量转换为自定义请求标头:
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
#6
If the request was a redirect, there should be a header indicating where it was redirected from.
如果请求是重定向,则应该有一个标题指示重定向的位置。