【问题标题】:Language browser detection and action rewrite语言浏览器检测和动作重写
【发布时间】:2022-01-13 12:16:30
【问题描述】:

到目前为止,我一直在使用 htaccess 仅使用此代码重写 ?action

RewriteCond %{QUERY_STRING} ^action=([^&\s]+)$
RewriteRule ^(?:index\.php|)$ /%1? [R=301,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\s\/]+)/?$ index.php?action=$1&r [L,QSA]

现在我进入了一个多语言网站,我想检测用户的浏览器语言,并根据这些信息将他重定向到他的语言版本

然后重写:

index.php?lang=en&action=subpage

进入

en/subpage

【问题讨论】:

  • 仅供参考 index.php?lang=en?action=subpage 无效,应为 index.php?lang=en&action=subpage
  • 有一个查询参数,因为?lang=endetect user´s browser language 不同。你必须决定要走哪条路线。
  • @anubhava ok 然后作为查询参数 ?lang=en :)

标签: php .htaccess locale


【解决方案1】:

您可以在站点根目录 .htaccess 中拥有这样的规则:

RewriteEngine On

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?lang=([^\s&]+)&action=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]

# add default language prefix
RewriteCond %{HTTP:Accept-Language} ^([a-z]{2})- [NC]
RewriteRule ^(?![a-z]{2}/)[^/]*/?$ /%1%{REQUEST_URI} [L,R=301,NE]

# internal forward from pretty URL to actual one
RewriteRule ^([a-z]+)/([\w-]+)/?$ index.php?lang=$1&action=$2 [L,QSA,NC]

【讨论】:

  • 谢谢,这很有效,我可以使用它,非常感谢。还有一件事,当有人进入网站时,是否可以使用语言制作默认网址?因此,当我进入 stackoverflow.com 时,它将重定向到 stackoverflow.com/en
  • 请务必查看我更新的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-11
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 2011-04-15
  • 2017-05-31
  • 1970-01-01
相关资源
最近更新 更多