.url 是 Windows 系統的網際網路捷徑檔案格式,內容為純文字 INI 結構。使用者下載後雙擊即可在預設瀏覽器開啟指定網址,常用於「加入我的最愛」「桌面捷徑」等行銷場景。
<?php // 定義捷徑內容(INI 格式) $shortcut = <<關鍵點解析
標頭/參數 說明 Content-Type: application/octet-stream通用二進位串流,強制瀏覽器下載而非開啟 Content-Disposition: attachment指定為附件下載, filename設定預設檔名filename編碼中文檔名建議用 rawurlencode()或 RFC 5987 格式:filename*=UTF-8''%E9%A2%A8%E7%81%AB%E7%A8%8B%E5%BC%8F%E8%A8%AD%E8%A8%88%E5%B8%AB.urlINI 結構必要欄位 URL(必填),IDList、IconFile、IconIndex等為選填進階:動態產生捷徑
<?php function generateUrlShortcut(string $url, string $title, string $icon = ''): string { $content = "[InternetShortcut]\nURL={$url}\n"; if ($icon) { $content .= "IconFile={$icon}\nIconIndex=0\n"; } return $content; } // 使用範例 $url = 'https://example.com/product/123'; $title = '熱銷商品'; header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . rawurlencode($title) . '.url"'); echo generateUrlShortcut($url, $title); ?>應用場景
- 行銷活動頁「一鍵加入桌面」
- 會員中心「我的收藏」匯出
- 客服傳送特定頁面連結給非技術用戶
- 離線 QR Code 掃描後自動儲存捷徑
只需幾行程式碼,即可提供原生桌面捷徑體驗,提升用戶留存與回訪率。