.htaccess - Htaccess URL Rewrite 捕获 2 或 5 个字符(但不是 3 或 4)

标签 .htaccess mod-rewrite

我当前捕获所选语言的重写规则是这样的:

RewriteRule ^language/([A-Za-z-]{2,5})/?$ /yadayada.php?hl=$1 [QSA,L]

所以它会捕获这些:

  • /language/es(西类牙语)
  • /language/de(德语)

但是,语言代码只能包含 2 个字母(例如:en、es、de)或 5 个字母(例如:zh-TW)。问题是它不应该接受以下任何一个(3 或 4 个字母,或全部 5 个小写字母):

  • /language/esp(三个字母)
  • /language/zh-tw(第二部分用小写,必须是zh-TW)

有什么想法吗?谢谢!

最佳答案

然后我会把它分开:

RewriteRule ^language/([a-z]{2}|[a-z]{2}-[A-Z]{2})/?$ /yadayada.php?hl=$1 [QSA,L]

如果有 2 个小写字母(例如“de”或“en”或“fr”)则匹配如果有 2 个小写字母后跟破折号和 2 个大写字母(例如“zh” -TW' 或 'zh-CN').

关于.htaccess - Htaccess URL Rewrite 捕获 2 或 5 个字符(但不是 3 或 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8287744/

相关文章:

php - 使用 htaccess 进行自动版本控制 : htaccess regex Rewrite rule not picking up pattern

php - 使用 .htaccess 永久重定向到子文件夹

javascript - 如何使用动态 url (slugs) 和 ng-include 在 Angular JS 中正确返回 404?

php - 使用 `.htaccess` RewriteEngine 规则在 php 中获取值

regex - 将所有流量重定向到另一个域的根

apache - Htaccess 重定向 301 从 "with parameter"到 "without parameter"

.htaccess - 使用 .htaccess 删除 .php 扩展名而不破坏 DirectoryIndex

regex - 如何结合两个 Apache htaccess 重定向条件(http 到 https 和非 www 到 www)

.htaccess 多语言(别名 URL/友好 URL)

.htaccess - 重写本地主机和实时环境的规则