透過 PHP 動態產生 .url 檔案,讓使用者點選下載後即可在桌面建立網站捷徑。這在行銷活動、快速部署內部系統連結等情境相當實用。
.url 實為純文字檔,內容遵循 InternetShortcut 格式。瀏覽器下載時若設定正確的 Content-Type 與 Content-Disposition,作業系統會自動識別為捷徑。
<?php
$shortCut = "[InternetShortcut]
URL=https://www.example.com
IDList=[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2";
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="MySite.url"');
echo $shortCut;
?>
favicon.ico,捷徑圖示才會正常顯示。<?php
$userId = isset($_GET['uid']) ? intval($_GET['uid']) : 0;
$targetUrl = "https://app.example.com/dashboard?uid={$userId}";
$safeName = "MyApp_{$userId}.url";
$content = "[InternetShortcut]\nURL={$targetUrl}\nIDList=[{000214A0-0000-0000-C000-000000000046}]\nProp3=19,2";
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"{$safeName}\"");
echo $content;
?>
使用者造訪 gen_shortcut.php?uid=123 即可下載專屬捷徑,適用於多租戶系統或個人化入口。
favicon.ico,且可公開存取。
filename*=UTF-8''%E6%8D%B7%E5%BE%91.url。
.url 為 Windows 專用格式。跨平台建議改用 .desktop (Linux) 或 .webloc (macOS)。
這項技巧零依賴、無需 JavaScript,極適合快速佈署「一鍵加入桌面」功能。