寻求非常规.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.comshould be sent to their regional subdomain (likexx.foo.com) via your geo-redirect logic - Traffic coming from any of your subdomains (e.g., users jumping to
foo.comfor 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:
RewriteEngine On: Turns on Apache's URL rewriting functionality—this is non-negotiable for any redirect rules to work.- 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. - First
RewriteCond: Checks if the request is targeting the mainfoo.comdomain (the[NC]flag makes this case-insensitive, soFOO.COMorFoo.Comalso match). We only want to redirect traffic hitting the main domain, not subdomains. - Second
RewriteCond: Excludes requests that come from anyfoo.comsubdomain. The!means "not matching",https?://covers both HTTP and HTTPS, and[^/]+catches any subdomain prefix (likexxorxy). So if someone clicks a link fromxx.foo.comtofoo.com, this condition fails and the redirect doesn't run. 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 (sofoo.com/shoesbecomesxx.foo.com/shoes), uses a temporary302redirect (switch to301once 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
相关产品推荐
相关产品推荐

