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

通过.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=XX parameter, permanently redirecting them to the clean prefixed version (e.g., example.com/dir/?lang=en → example.com/en/dir/). Using R=301 tells 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 en or de), then rewrites the internal request to strip the prefix and add the ?lang=XX parameter. The !-f and !-d checks ensure we leave real files (like page.php) and directories untouched, while QSA preserves 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 to example.com/main/?lang=de without breaking directory behavior.
Key Notes for Smooth Setup
  • Enable mod_rewrite: Make sure your Apache server has the mod_rewrite module 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 (like en-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/), change RewriteBase / to RewriteBase /blog/ to keep paths aligned.
  • Test with Temporary Redirects: When first setting up, use R=302 instead of R=301 to avoid browser caching incorrect redirects. Once you confirm everything works, switch back to R=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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 13:08:11