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

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 with en. The (.*) captures everything after en (whether it's a subdirectory, filename, or even nothing) and stores it in the $1 variable.
  • https://www.mywebsite.com/en$1: We append the captured $1 to the target domain's /en path, so /en/page1 becomes /en/page1 on 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=301 sets a permanent redirect (great for SEO, as it tells search engines the page has moved permanently), and L tells 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:51:45