Regex 本地化重写条件
我有以下类型的URLRegex 本地化重写条件,regex,apache,url,mod-rewrite,url-rewriting,Regex,Apache,Url,Mod Rewrite,Url Rewriting,我有以下类型的URL foo.com/en-us/ foo.com/fr-fr/ URL也可以是: foo.com/en-us/index.html foo.com/en-us/anything/anything/index.html 我想重写并重定向到小写版本,如: foo.com/en-US/ to foo.com/en-us/ 或 或 我尝试了不同的跟随方式,但没有成功 ^.*/[A-Z]{2}-[A-Z]{2}\/.不包括这些情况 它包括:foo.com/EN-US/index
foo.com/en-us/
foo.com/fr-fr/
URL也可以是:
foo.com/en-us/index.html
foo.com/en-us/anything/anything/index.html
我想重写并重定向到小写版本,如:
foo.com/en-US/
to
foo.com/en-us/
或
或
我尝试了不同的跟随方式,但没有成功
^.*/[A-Z]{2}-[A-Z]{2}\/.
不包括这些情况
它包括:foo.com/EN-US/index.html
这种情况下的重写条件和规则是什么?您可以使用此
重写映射和重写规则重定向到小写/-
URL:
# RewriteMap to convert any string to lowercase
# This cannot be used in .htaccess
RewriteMap lc int:tolower
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/[a-z-]*[A-Z]
RewriteRule ^/?([a-z]{2}-[a-z]{2})(/.*)?$ /${lc:$1}$2 [L,NC,R=301,NE]
这里使用RewriteCond%{REQUEST_URI}^/[a-z-]*[a-z]
来确保第一部分中至少有一个大写字母。您应该在正则表达式中包含小写字母。尝试将其用作正则表达式:
^.*\/[a-zA-Z]{2}-[a-zA-Z]{2}+.*
我如何使其更一般地包括fr
、it
或任何国家/地区?请查看我的更新答案。要使其完全通用,您需要使用RewriteMap
。为此,您需要访问Apache服务器配置。你有吗?是的,我有权访问httpd.conf。我试过:RewriteMap lc int:tolower#RewriteCond%{REQUEST_URI}^.*\/[a-zA-Z]{2}-[a-zA-Z]{2}\/.#RewriteRule${lc:%%{REQUEST_URI}[R=301,L]
但是运气不好。你能不能也介绍一下像/en-Us/index.html这样的内容
# RewriteMap to convert any string to lowercase
# This cannot be used in .htaccess
RewriteMap lc int:tolower
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/[a-z-]*[A-Z]
RewriteRule ^/?([a-z]{2}-[a-z]{2})(/.*)?$ /${lc:$1}$2 [L,NC,R=301,NE]
^.*\/[a-zA-Z]{2}-[a-zA-Z]{2}+.*