.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 formatid=fileID, wherefileIDis 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-archiveor/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 originalid=fileIDparameter gets passed todownload.phpseamlessly.
Quick Checks to Avoid Headaches
- Ensure your Apache virtual host configuration has
AllowOverride Allset for your website directory. Without this, Apache will ignore.htaccessfiles 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-fileIDand it should loaddownload.php?id=your-test-fileIDwithout showing a 404.
内容的提问来源于stack exchange,提问作者B Happy
相关产品推荐
相关产品推荐

