You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为短链接设置指定过期时间(如80小时)?

Hey there! Great question—lots of folks assume all short links are permanent, so it makes total sense you'd want one that expires after a set time like 80 hours. Here are a few reliable ways to pull this off:

Most mainstream short link tools offer expiry configuration right out of the box, no tech skills required:

  • Look for services that include an expiration time or custom validity option. When generating your short link, just set the expiry to 80 hours—this is usually tucked away in the "advanced settings" section of the creation interface. Some even let you set a custom redirect page for when the link expires (like a "link no longer valid" message).
  • Note: Free tiers might have limits on maximum expiry duration, but 80 hours is short enough that most free plans will cover it. If not, a basic paid tier will likely unlock this feature.

If you don't want to rely on third-party services, you can whip up a simple solution with minimal code:

  • Use a lightweight backend framework (like Node.js/Express or Python/Flask) to create a small service:
    1. Store the original URL, expiry timestamp, and a randomly generated short code (use a database in production, not just in-memory storage like the example below).
    2. When someone visits the short link, check if the current time is past the expiry timestamp:
      • If not, redirect them to the original URL.
      • If yes, show a "link expired" message.
  • Here's a super basic Node.js/Express example to get you started:
const express = require('express');
const app = express();

// In-memory storage (replace with a database like SQLite/PostgreSQL for production)
const shortLinks = {
  "xyz789": {
    originalUrl: "https://your-long-target-url.com",
    expiryTimestamp: Date.now() + 80 * 60 * 60 * 1000 // 80 hours from now in milliseconds
  }
};

app.get('/:shortCode', (req, res) => {
  const linkData = shortLinks[req.params.shortCode];
  
  if (!linkData) {
    return res.send("This short link doesn't exist.");
  }
  
  if (Date.now() > linkData.expiryTimestamp) {
    return res.send("This link has expired.");
  }
  
  res.redirect(linkData.originalUrl);
});

app.listen(3000, () => {
  console.log("Expiring short link service running on http://localhost:3000");
});
  • This approach gives you full control over expiry behavior and eliminates reliance on third-party platforms.

Many short link services offer API access that lets you set expiry times programmatically:

  • When making an API request to generate a short link, include a parameter like expires_at (set to the exact timestamp when you want the link to expire) or ttl (time-to-live, set to 80 * 3600 = 288000 seconds).
  • This is perfect if you need to generate expiring links in bulk or integrate the functionality into your own app.

Quick Tip

Whichever method you choose, do a quick test first—set a 1-minute expiry on a test link, wait a minute, and confirm it properly expires. This ensures everything works as expected for your 80-hour timeline.

内容的提问来源于stack exchange,提问作者Kalpashree Bal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:23:42