mod_rewrite 正则表达式重写 url 的部分内容
我需要将部分网址重写为小写。我可以重写 url 中的所有字符:
RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [L,R=301]
但是,我有一个特殊情况。例如; http://somedomain.com/monitors/服务/awe/awe-prod/awr02.awe-prod.a/Read_job_245/。我想继续将所有字符重写为小写,例如,除了 Read_job_245 之外。 Read_job_245 可以是任何大小写字母或数字,并且始终是第六级。
我无法弄清楚 mod_rewrite 部分。
预先感谢您的任何帮助。
I need to rewrite parts of the url to lowercase. I can rewrite all characters in a url with:
RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [L,R=301]
But, I have a special case. For example; http://somedomain.com/monitors/services/awe/awe-prod/awr02.awe-prod.a/Read_job_245/. I want to continue to rewrite all characters to lowercase, except Read_job_245 for example. Read_job_245 can be any letters or number in upper or lower case, and will always be the at the sixth level.
I can't figure out the mod_rewrite portion.
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的意思是“我想重写任何级别除了第六个”,那么这应该可以工作:(
注意 URL 以“/”开头,因为 RewriteMap 不起作用,因此似乎不起作用在我的 .htaccess 中)
它将获取前五个匹配并将它们小写,然后它将按原样保留第六个,然后它将小写任何后续级别(如果存在)。
如果你只达到 6 个级别(并且第六个级别永远不应该被重写),那么这应该足够了:
RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [AZ]
RewriteCond %{REQUEST_URI} !^((/[^AZ/]+){5})(/[^/][AZ][^/])/?$
RewriteRule ^((/[^/]+){1,5})(/[^/]+)/? ${lc:$1}$4 [L,R=301]
测试:
逐行解释:
If you mean "I want to rewrite any level except the sixth" then this should work:
(Note the URLs begin with "/" because RewriteMap won't work in and so doesn't seem to work in .htaccess for me either)
It'll take the first five matches and lower-case them, then it'll leave the sixth as-is, then it'll lower-case any subsequent levels (if they exist).
If you only ever get to 6 levels (and the sixth should never be rewritten) then this should be adequate:
RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_URI} !^((/[^A-Z/]+){5})(/[^/][A-Z][^/])/?$
RewriteRule ^((/[^/]+){1,5})(/[^/]+)/? ${lc:$1}$4 [L,R=301]
Tested with:
Line by line explanation: