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

.htaccess重写配置:将伪静态下载链接映射至真实PHP文件

Fixing the /file?id=fileID to download.php Rewrite Rule

Got it, let's get that pseudo-static URL working properly. The issue is your Apache server isn't mapping /file?id=fileID to the actual download.php?id=fileID endpoint, hence the 404 error. Here's how to fix it step by step:

Prerequisite First

Make sure Apache's mod_rewrite module is enabled on your server. Most hosting environments have this on by default, but if you're managing your own server, you might need to enable it via a2enmod rewrite (on Debian/Ubuntu) and restart Apache afterward.

Add the Rewrite Rules to .htaccess

Open (or create if it doesn't exist) the .htaccess file in your website's root directory (the same folder where download.php and functions.php live). Paste these rules:

RewriteEngine On

# Map /file?id=fileID to download.php?id=fileID
RewriteCond %{QUERY_STRING} ^id=([a-zA-Z0-9]+)$
RewriteRule ^file$ download.php [L,QSA]

What Each Part Does:

  • RewriteEngine On: Turns on Apache's URL rewriting engine (required for any rewrite rules to function).
  • RewriteCond %{QUERY_STRING} ^id=([a-zA-Z0-9]+)$: Checks that the query string exactly matches the format id=fileID, where fileID is made of letters and numbers. If your file IDs use other characters (like underscores or hyphens), update the regex to ^id=([a-zA-Z0-9_-]+)$.
  • RewriteRule ^file$ download.php [L,QSA]:
    • ^file$: Exact match for the path /file (so it won't accidentally match paths like /file-archive or /file123).
    • download.php: The actual file we want to route the request to.
    • [L]: Tells Apache this is the last rule to process if it matches (prevents conflicts with other existing rules).
    • [QSA]: Stands for "Query String Append" — this ensures the original id=fileID parameter gets passed to download.php seamlessly.

Quick Checks to Avoid Headaches

  • Ensure your Apache virtual host configuration has AllowOverride All set for your website directory. Without this, Apache will ignore .htaccess files entirely.
  • If you already have other rewrite rules in .htaccess, place this rule before any existing ones to prevent it from being overridden.
  • Test the URL immediately after saving: Visit http://www.example.net/file?id=your-test-fileID and it should load download.php?id=your-test-fileID without showing a 404.

内容的提问来源于stack exchange,提问作者B Happy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:07:35