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

移除重定向附加查询参数,WordPress .htaccess子域名重定向问题

Fix: Remove Query Parameters When Redirecting Subdomain to Main Domain in 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.

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 (the NC flag makes it case-insensitive, so it works for SubDomain.Example.Net too).
  • 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 /$1 if you don't want to keep the path).
    • R=301: Marks this as a permanent redirect (use R=302 for 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

  1. Save your .htaccess file and upload it back to your server (if edited locally).
  2. Clear your browser cache before testing—301 redirects are cached aggressively, so old behavior might still show up otherwise.
  3. Test both scenarios:
    • subdomain.example.net/anystring should redirect to example.net/anystring
    • subdomain.example.net/anystring?anytext=foo should redirect to example.net/anystring (no query parameters attached)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:36:24