移除重定向附加查询参数,WordPress .htaccess子域名重定向问题
Got it, let's sort out this query string issue with your subdomain redirect in WordPress's .htaccess. The core problem here is that your current setup isn't explicitly telling Apache to discard query parameters during the redirect—we'll fix that with targeted rewrite rules.
First, critical note: always add custom redirect rules BEFORE the default WordPress .htaccess block (the section wrapped in # BEGIN WordPress and # END WordPress). WordPress's internal routing rules will override anything placed after them, so putting your redirect first ensures it runs properly.
Solution 1: For Apache 2.4+ (Recommended)
This uses the QSD (Query String Discard) flag, the cleanest way to drop all query parameters:
# Redirect subdomain to main domain, discard all query parameters RewriteEngine On RewriteCond %{HTTP_HOST} ^subdomain\.example\.net$ [NC] RewriteRule ^(.*)$ https://example.net/$1 [R=301,L,QSD]
Rule Breakdown:
RewriteEngine On: Enables mod_rewrite (required for these rules to work).RewriteCond %{HTTP_HOST} ^subdomain\.example\.net$ [NC]: Matches requests to your subdomain (theNCflag makes it case-insensitive, so it works forSubDomain.Example.Nettoo).RewriteRule ^(.*)$ https://example.net/$1 [R=301,L,QSD]:^(.*)$: Captures any path after the subdomain (like/anystring).https://example.net/$1: Redirects to the main domain while preserving the path (remove/$1if you don't want to keep the path).R=301: Marks this as a permanent redirect (useR=302for temporary redirects while testing).L: Stops processing further rules after this one.QSD: The key fix—this tells Apache to discard all query parameters from the original request.
Solution 2: For Older Apache Versions (Pre-2.4)
If your server runs Apache 2.2 or earlier (where QSD isn't supported), you can explicitly clear the query string by adding a ? at the end of the target URL:
# Redirect subdomain to main domain, clear query parameters (Apache 2.2 compatible) RewriteEngine On RewriteCond %{HTTP_HOST} ^subdomain\.example\.net$ [NC] RewriteRule ^(.*)$ https://example.net/$1? [R=301,L]
The trailing ? tells Apache not to pass any query parameters from the original request to the target URL.
Post-Setup Checks
- Save your .htaccess file and upload it back to your server (if edited locally).
- Clear your browser cache before testing—301 redirects are cached aggressively, so old behavior might still show up otherwise.
- Test both scenarios:
subdomain.example.net/anystringshould redirect toexample.net/anystringsubdomain.example.net/anystring?anytext=fooshould redirect toexample.net/anystring(no query parameters attached)
内容的提问来源于stack exchange,提问作者Sharad Soni

