mod_rewrite 正则表达式重写 url 的部分内容

发布于 2024-12-14 09:13:41 字数 552 浏览 0 评论 0原文

我需要将部分网址重写为小写。我可以重写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

违心° 2024-12-21 09:13:41

如果您的意思是“我想重写任何级别除了第六个”,那么这应该可以工作:(

RewriteEngine On
RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_URI} !^((/[^A-Z/]+){5})(/[^/]*[A-Z][^/]*)(/[^A-Z/]+)*/?$
RewriteRule ^((/[^/]+){1,5})((/[^/]+)((/[^/]+)*))? ${lc:$1}$4${lc:$5} [L,R=301]

注意 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]

测试:

/monitors/services/awe/AWE --> /monitors/services/awe/awe
/monitors/services/awe/awe-prod/awr02.awe-prod.a/Read_job_245/ --> No change
/MONITORS/SERVICES/awe/awe-prod/awr02.awe-prod.a/Read_job_245/ --> /monitors/services/awe/awe-prod/awr02.awe-prod.a/Read_job_245/
/MONITORS/SERVICES/awe/awe-prod/awr02.awe-prod.a/Read_job_245/AAA/BBB/ --> /monitors/services/awe/awe-prod/awr02.awe-prod.a/Read_job_245/aaa/bbb/ (using 1st rule set)

逐行解释:

#Enable mod_rewrite
RewriteEngine On
#Create mapping to lower case
RewriteMap  lc int:tolower
#Match URLs with upper-case characters
RewriteCond %{REQUEST_URI} [A-Z]
#Stop rewrite loop when upper-case is in the allowed 6th level
RewriteCond %{REQUEST_URI} !^((/[^A-Z/]+){5})(/[^/]*[A-Z][^/]*)/?$
#Rewrite URL to lower case
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:

RewriteEngine On
RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_URI} !^((/[^A-Z/]+){5})(/[^/]*[A-Z][^/]*)(/[^A-Z/]+)*/?$
RewriteRule ^((/[^/]+){1,5})((/[^/]+)((/[^/]+)*))? ${lc:$1}$4${lc:$5} [L,R=301]

(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:

/monitors/services/awe/AWE --> /monitors/services/awe/awe
/monitors/services/awe/awe-prod/awr02.awe-prod.a/Read_job_245/ --> No change
/MONITORS/SERVICES/awe/awe-prod/awr02.awe-prod.a/Read_job_245/ --> /monitors/services/awe/awe-prod/awr02.awe-prod.a/Read_job_245/
/MONITORS/SERVICES/awe/awe-prod/awr02.awe-prod.a/Read_job_245/AAA/BBB/ --> /monitors/services/awe/awe-prod/awr02.awe-prod.a/Read_job_245/aaa/bbb/ (using 1st rule set)

Line by line explanation:

#Enable mod_rewrite
RewriteEngine On
#Create mapping to lower case
RewriteMap  lc int:tolower
#Match URLs with upper-case characters
RewriteCond %{REQUEST_URI} [A-Z]
#Stop rewrite loop when upper-case is in the allowed 6th level
RewriteCond %{REQUEST_URI} !^((/[^A-Z/]+){5})(/[^/]*[A-Z][^/]*)/?$
#Rewrite URL to lower case
RewriteRule ^((/[^/]+){1,5})(/[^/]+)/? ${lc:$1}$4 [L,R=301]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文