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

RewriteRule带额外查询参数时返回404问题求助

Fixing 404 Errors When Adding Query Params (like utm_source) to Rewritten Product URLs

Hey there! This is a super common gotcha with mod_rewrite, so let's break down why those UTM parameters are breaking your product pages and how to fix it.

Common Causes & Solutions

1. You're Missing the QSA Flag

The most likely culprit is that your rewrite rule isn't preserving the original query string. By default, when you rewrite a URL that already has query parameters in the target, mod_rewrite replaces the original query string instead of appending it.

Example of a problematic rule:

RewriteRule ^product/([0-9]+)$ product.php?id=$1 [L]

When someone visits /product/123?utm_source=google, this rule rewrites to product.php?id=123—the utm_source parameter gets dropped entirely. Depending on your server setup, this can lead to unexpected 404s if your product page relies on passing through those parameters, or if other rules conflict.

Fixed rule with QSA:

RewriteRule ^product/([0-9]+)$ product.php?id=$1 [L,QSA]

The QSA (Query String Append) flag tells mod_rewrite to add any original query parameters to the end of the target URL. Now /product/123?utm_source=google becomes product.php?id=123&utm_source=google, which works exactly as intended.

2. Your Rule Has a Condition Blocking Query Strings

If you added a RewriteCond that only triggers the rewrite when there are no query parameters, requests with UTM params will skip the rule entirely. For example:

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^product/([0-9]+)$ product.php?id=$1 [L]

This rule only runs when the query string is empty. When someone adds ?utm_source=facebook, the condition fails, and Apache tries to load the physical file /product/123—which doesn't exist, hence the 404.

Fix:
Remove the RewriteCond %{QUERY_STRING} ^$ line, or adjust it to allow query strings you care about. For example, to allow UTM parameters specifically:

RewriteCond %{QUERY_STRING} ^utm_ [OR]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^product/([0-9]+)$ product.php?id=$1 [L,QSA]

3. Your Pattern Isn't Matching Correctly

Double-check that your rewrite pattern is targeting the path only (mod_rewrite ignores query strings in the pattern by default). Make sure something like [^/]+ is correctly capturing your product IDs/slugs without accidentally excluding valid requests (though query strings won't interfere with the pattern match, it's worth verifying).

Full Working Example

Here's a complete .htaccess snippet that should handle rewritten product URLs with any query parameters:

RewriteEngine On

# Rewrite product URLs and preserve all query params
RewriteRule ^product/([0-9a-zA-Z-]+)$ product.php?id=$1 [L,QSA]

Debugging Tip

If you're still stuck, you can enable rewrite logging temporarily (don't leave this on in production!) to see exactly how Apache is processing your requests:

RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3

This will log every step of the rewrite process, so you can confirm if your rule is even being triggered for requests with query parameters.

Give these fixes a shot, and let me know if you need further help troubleshooting!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:26:05