因此,我使用 Laravel 5.4 构建了一个小型 URL 缩短器,其中有一个路由:domain.com/{urlkey} - 我获取密钥并在 Redis 中的 Laravel 缓存中查找它。我还有另一个密钥来跟踪访问次数 - 因此每当访问 URL 时我只需增加 :visits 键。
现在我注意到,当我将其中一个 URL 复制并粘贴到新选项卡中时...或者甚至从我的应用程序中单击它时,它会完全忽略我的代码。我可以放入 die(),它甚至不会停止!这永远不会触发键上的缓存增量......
有什么想法吗?可能遗漏了一些非常明显的东西。
简单的查找和增量如下:但我什至不认为它击中了代码?为什么会这样呢?
其他注意域是 https - 但是我已经在 http 和 https 上对此进行了测试
// retrieve redirect URL from cache
$redirectUrl = Cache::get('short:' . $shortKey);
// if we find the redirect in the cache - increment visits - 301 redirect
if($redirectUrl)
{
Cache::increment('short:' . $shortKey . ':visits', 1);
return redirect($redirectUrl, 301);
}
Run Code Online (Sandbox Code Playgroud)
因为301 = Moved Permanently,浏览器会缓存,下次自动重定向,不再调用原来的页面
如果我没记错的话,如果您希望重定向始终经过原始页面,则需要将 301 替换为302 Found