301重定向之前的过程数据? [英] Process data BEFORE a 301 Redirect?

查看:111
本文介绍了301重定向之前的过程数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直在研究PHP链接缩短器(我知道,这正是世界所需要的).基本上,当页面加载时,php确定需要去的地方并发送301标头来重定向浏览器,就像这样...

So, I've been working on a PHP link shortener (I know, just what the world needs). Basically when the page loads, php determines where it needs to go and sends a 301 Header to redirect the browser, like so...

Header( "HTTP/1.1 301 Moved Permanently" );
header("Location: http://newsite.com";

现在,我正在尝试对重定向添加一些跟踪,并在重定向发生之前将一些自定义分析数据插入MySQL表中.如果我不指定重定向类型,而只需使用:

Now, I'm trying to add some tracking to my redirects and insert some custom analytics data into a MySQL table before the redirect happen. It works perfectly if I don't specify the a redirect type and just use:

header("Location: http://newsite.com";

但是,当然,一旦您添加了301标头,就不会处理其他任何事情.实际上,在第一个请求时,它将数据发送到MySQL,但是在任何后续请求中,都与数据库没有通信.

But, of course as soon as you add in the 301 header, nothing else gets processed. Actually, on the first request, it sends the data to MySQL, but on any subsequent requests there's no communication with the database.

我认为这是一个浏览器缓存问题,一旦看到301,它就决定没有理由在将来的请求中解析任何内容.但是,有谁知道是否有任何办法可以解决这个问题?

I assume it's a browser caching issue, once it's seen the 301 it decides they're no reason to parse anything on future requests. But, does anyone know if there's any way to get around this?

出于SEO的目的,我真的很想将其保留为301(我相信您是否未指定默认情况下会发送404?).

I'd really like to keep it as a 301 for SEO purposes (I believe if you don't specify it sends a 404 by default?).

我曾考虑过使用.htaccess在要运行MySQL的页面上添加一个文件,但是使用301时,会不会被忽略呢?

I thought about using .htaccess to prepend a file to the page that will do the MySQL work, but with the 301, wouldn't that just get ignored as well?

无论如何,除了使用不同类型的重定向外,我不确定是否有其他解决方案,但是我已经准备放弃.因此,任何建议将不胜感激.谢谢!

Anyway, I'm not sure if there's any solution other than using a different type of redirect, but I'm ready to give up just yet. So, any suggestions would be much appreciated. Thanks!

推荐答案

尝试在第一个标头语句之前添加以下内容;这应该可以防止在典型页面中进行缓存,但是我不确定它是否可用于重定向:

Try adding the following before the first header statement; this should prevent caching in typical pages, but I'm not sure if it works for redirects:

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Thu, 1 Jan 1970 00:00:00 GMT");

这篇关于301重定向之前的过程数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆