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

寻求非常规.htaccess方案:主域名重定向排除子域名来源流量

Solution for .htaccess Redirect: Send Main Domain Traffic to Regional Subdomains (Exclude Subdomain Referrals)

Got it, let's break down how to solve this redirect challenge for your platform. Your core needs are clear:

  • Direct visitors to foo.com should be sent to their regional subdomain (like xx.foo.com) via your geo-redirect logic
  • Traffic coming from any of your subdomains (e.g., users jumping to foo.com for checkout) needs to stay on the main domain without being redirected

Here's a robust .htaccess rule set that handles this, plus explanations and edge-case fixes:

# Enable rewrite engine (required for all rules)
RewriteEngine On

# Optional: Prioritize checkout path access (skip redirect for checkout flows)
RewriteCond %{HTTP_HOST} ^foo\.com$ [NC]
RewriteRule ^checkout/ - [L]

# Core rule: Redirect main domain traffic ONLY if not coming from a subdomain
RewriteCond %{HTTP_HOST} ^foo\.com$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://[^/]+\.foo\.com/ [NC]
RewriteRule ^(.*)$ https://%{ENV:GEO_COUNTRY}.foo.com/$1 [R=302,L]

Let's walk through what each part does:

  1. RewriteEngine On: Turns on Apache's URL rewriting functionality—this is non-negotiable for any redirect rules to work.
  2. Optional Checkout Exclusion: If your checkout lives at a specific path like foo.com/checkout/, this rule tells Apache to skip any redirect logic for those requests. The - means "do nothing" and [L] stops processing further rules, ensuring checkout flows aren't interrupted.
  3. First RewriteCond: Checks if the request is targeting the main foo.com domain (the [NC] flag makes this case-insensitive, so FOO.COM or Foo.Com also match). We only want to redirect traffic hitting the main domain, not subdomains.
  4. Second RewriteCond: Excludes requests that come from any foo.com subdomain. The ! means "not matching", https?:// covers both HTTP and HTTPS, and [^/]+ catches any subdomain prefix (like xx or xy). So if someone clicks a link from xx.foo.com to foo.com, this condition fails and the redirect doesn't run.
  5. RewriteRule: This does the actual redirect. It sends the request to your geo-targeted subdomain (using %{ENV:GEO_COUNTRY}—replace this with your actual geo-lookup variable if needed), preserves the original path (so foo.com/shoes becomes xx.foo.com/shoes), uses a temporary 302 redirect (switch to 301 once you've tested and confirmed it works), and [L] ensures no other rules run after this one.

Bonus: Fix for browsers that block Referer headers

Some users have privacy settings that strip the Referer header, which would break the rule above. For a more reliable solution, use a cookie to track if someone came from a subdomain:

On your subdomain's .htaccess:

RewriteEngine On
# Set a cookie that marks the user as coming from a subdomain
RewriteRule ^ - [CO=from_subdomain:1:foo.com]

On your main domain's .htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^foo\.com$ [NC]
RewriteRule ^checkout/ - [L]

# Redirect only if the subdomain cookie doesn't exist
RewriteCond %{HTTP_HOST} ^foo\.com$ [NC]
RewriteCond %{HTTP_COOKIE} !from_subdomain=1 [NC]
RewriteRule ^(.*)$ https://%{ENV:GEO_COUNTRY}.foo.com/$1 [R=302,L]

This cookie-based method avoids relying on the Referer header, making it more consistent across all user browsers and privacy settings.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:49:39