HTAccess重写需求:语言路径触发跨域名跳转(子目录失效问题)
Fixing Domain Rewrite for Language Switch Subdirectories
Hey there! The issue with your current rule is that it only matches the exact path ^en$ — meaning it only works for the root /en page, not any subdirectories like /en/page1. Let's tweak the rule to catch all paths starting with en, no matter what comes after.
Here's the corrected Apache mod_rewrite rule:
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.de$ RewriteRule ^en(.*)$ https://www.mywebsite.com/en$1?%{QUERY_STRING} [R=301,L]
Let me break down what each part does:
^en(.*)$: This regex matches any URL path that starts withen. The(.*)captures everything afteren(whether it's a subdirectory, filename, or even nothing) and stores it in the$1variable.https://www.mywebsite.com/en$1: We append the captured$1to the target domain's/enpath, so/en/page1becomes/en/page1on the new domain — preserving your full URL structure perfectly.?%{QUERY_STRING}: This ensures any query parameters from the original URL (like?utm_source=foo) get carried over to the redirected URL, so you don't lose any tracking or dynamic data.[R=301,L]:R=301sets a permanent redirect (great for SEO, as it tells search engines the page has moved permanently), andLtells mod_rewrite this is the last rule to process once matched.
Quick tip: If you're testing this out, you can use R=302 instead of 301 first — that's a temporary redirect, so you don't accidentally cache a wrong rule in browsers or search engines. Once you confirm everything works, switch back to 301.
内容的提问来源于stack exchange,提问作者onclesam
相关产品推荐
相关产品推荐

