WordPress(WPML)多语言站点.htaccess规则:URL特定字符串忽略配置
Got it, let's sort out this .htaccess rule for your WPML-powered bilingual WordPress site. The goal here is to internally rewrite requests to /en/xyz to serve the content from /xyz while keeping the original URL intact—here's how to make it work:
Step 1: Add the Custom Rewrite Rule
Insert this code before the default WordPress .htaccess block (the one wrapped in # BEGIN WordPress and # END WordPress). This ensures our rule runs first before WordPress takes over all request handling:
# BEGIN XYZ Page Language Exception <IfModule mod_rewrite.c> RewriteEngine On # Internally rewrite /en/xyz (and /en/xyz/) to /xyz, preserving any extra path segments RewriteRule ^en/xyz(/.*)?$ /xyz$1 [L,NC,PT] </IfModule> # END XYZ Page Language Exception
Let's Break Down the Rule
^en/xyz(/.*)?$: This pattern matches URLs starting withen/xyz, including optional trailing slashes or extra path segments (like/en/xyz/subpageif you ever need it)./xyz$1: This is the target path—we're pointing the request to the main German version of the page, with$1capturing and preserving any extra parts of the URL afterxyz.[L,NC,PT]:L: Marks this as the last rule to process if the match is found, preventing further unnecessary rewrites.NC: Makes the match case-insensitive (useful if someone types/EN/XYZby mistake).PT: Passes the rewritten path to the next phase of processing, so WordPress can correctly recognize and serve the/xyzpage content without breaking things.
Important Notes
- Clear Cache: After updating .htaccess, clear your browser cache or test in incognito mode to avoid cached redirects messing with your changes.
- WPML Compatibility: Since WPML handles language routing via its own rules, placing our custom rule before WordPress's default block ensures it takes priority without conflicting with WPML's core logic.
- Query Strings: Any query parameters (like
/en/xyz?foo=bar) will automatically be preserved and passed to the/xyzpage—no extra configuration needed for that.
内容的提问来源于stack exchange,提问作者Akshat
相关产品推荐
相关产品推荐

