通过.htaccess实现网站多语言支持的URL重写方案咨询
Absolutely! You can pull off this clean language-prefixed URL setup using .htaccess and mod_rewrite—no need to tweak rules every time you add a new directory. Let’s break this down step by step, with rules that work for any existing or future content:
Core
.htaccess Rules Drop these rules into your root .htaccess file:
# Enable mod_rewrite functionality RewriteEngine On RewriteBase / # Optional (but SEO-friendly): Redirect old ?lang=XX URLs to clean language-prefixed ones RewriteCond %{THE_REQUEST} \s/+(.*?)(?:\?|&)lang=([a-z]{2})(?:&|\s|$) [NC] RewriteRule ^ /%2/%1? [R=301,L] # Rewrite clean language-prefixed URLs to internal ?lang=XX format (for non-files/dirs) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([a-z]{2})/(.*)$ /$2?lang=$1 [L,QSA] # Handle directory requests (e.g., /en/dir/ → /dir/?lang=en) RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^([a-z]{2})/(.*)/$ /$2/?lang=$1 [L,QSA]
What Each Rule Does
Let’s unpack this so you understand exactly what’s happening:
- Redirect Rule (Optional):This fixes old URLs with the
?lang=XXparameter, permanently redirecting them to the clean prefixed version (e.g.,example.com/dir/?lang=en→example.com/en/dir/). UsingR=301tells search engines to update their indexes, avoiding duplicate content issues. - Non-File/Directory Rewrite:This matches any URL starting with a 2-letter language code (like
enorde), then rewrites the internal request to strip the prefix and add the?lang=XXparameter. The!-fand!-dchecks ensure we leave real files (likepage.php) and directories untouched, whileQSApreserves any extra query parameters (e.g.,example.com/en/dir/?foo=bar→example.com/dir/?lang=en&foo=bar). - Directory Handling Rule:This specifically targets directory URLs ending with a slash (like
example.com/de/main/), ensuring they’re rewritten correctly toexample.com/main/?lang=dewithout breaking directory behavior.
Key Notes for Smooth Setup
- Enable mod_rewrite: Make sure your Apache server has the
mod_rewritemodule enabled. Most shared hosts have this on by default, but if not, reach out to your provider or enable it in your Apache config. - Adjust Language Code Regex: The rules use
[a-z]{2}for 2-letter language codes. If you need support for regional codes (likeen-US), update the regex to[a-z]{2}-[A-Z]{2}. - Subdirectory Hosting: If your site lives in a subdirectory (e.g.,
example.com/blog/), changeRewriteBase /toRewriteBase /blog/to keep paths aligned. - Test with Temporary Redirects: When first setting up, use
R=302instead ofR=301to avoid browser caching incorrect redirects. Once you confirm everything works, switch back toR=301.
With these rules in place, any new directories or files you add later will automatically work with the language-prefixed URLs—no need to edit .htaccess again. Your internal code can keep using the ?lang=XX parameter exactly as before, while users get clean, intuitive URLs.
内容的提问来源于stack exchange,提问作者Danny Franklin
相关产品推荐
相关产品推荐

