在 301 重定向之前处理数据?

发布于 2024-09-03 11:00:21 字数 709 浏览 6 评论 0原文

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

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

现在,我尝试向我的重定向添加一些跟踪并将一些自定义分析数据插入 MySQL重定向发生之前的表。如果我不指定重定向类型而只使用:,它就可以完美地工作:

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

但是,当然,一旦您添加了 301 标头,就不会再处理任何其他内容。实际上,在第一个请求中,它将数据发送到 MySQL,但在任何后续请求中,都不会与数据库进行通信。

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

我真的很想将其保留为 301 以用于 SEO 目的(我相信如果您不指定它会默认发送 404?)。

我考虑过使用 .htaccess 在将执行 MySQL 工作的页面前面添加一个文件,但是使用 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";

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";

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.

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?

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?).

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

在风中等你 2024-09-10 11:00:21

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

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

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");
安穩 2024-09-10 11:00:21

解释在 301 代码的描述中:“已移动永久”您明确告诉浏览器新页面是永久的新位置,因此它没有理由访问该页面又是旧网址。

相反,请使用 303 查看其他状态。这具有大致相同的含义(因为它将访问者重定向到其他地方),但它“不得”被缓存。

The explanation is in the description of the 301 code: "Moved Permanently" You're specifically telling the browser that the new page is a permanent new location, and therefore there's no reason for it to ever visit the old URL again.

Instead, use a 303 See Other status. This has roughly the same meaning (in that it redirects the visitor elsewhere), but it "must not" be cached.

零度° 2024-09-10 11:00:21

您应该使用默认的 302 重定向,这是一个临时重定向,不会被缓存。

301 是永久重定向,大多数浏览器都会缓存它。

You should use the default 302 redirect, which is a temporary redirect and will not be cached.

301 is the permanent redirect and most browser will cache it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文