简体   繁体   中英

Apache mod_rewrite lowercase a part of REQUEST_URI

I need some help with rewriting an url to lowercase.
So, i have an apache server which is a proxy for a couple of applications (some on IIS and some on JBoss). The JBoss app has app context.
The problem is if i try to access an app over https://www.domain.com/App i get a 404.
If i try https://www.domain.com/app all is fine.
If i do a catch all RewriteRule to lowercase all uppercase then all my links and scripts get lowercase which is not really working.
In that case, a https://www.domain.com/app/SomeFolder/SomeScript.js link, would look like this https://www.domain.com/app/somefolder/somescript.js .
What i want is, if a https://www.domain.com/ ApP /SomeFolder/SomeScript.js url is entered, to change it to https://www.domain.com/ app /SomeFolder/SomeScript.js
My Apache config to catch uppercase letters and lowercase them is:

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

But this doesn't work and i can't seem to find the correct regex for the cond to catch only the app context and not the whole REQUEST_URI as my log says not matched.

Hope all this made sense. If more info is needed i will provide it.
Ty again.

EDIT:
The (?<=.com/)[^/\\s]+ regex catches what i need. The question now will Apache work with this regex.
SO, the new ruleset should look like this(?):

RewriteMap  lc int:tolower
RewriteRule ((?<=\.com/))([^/\s]+) ${lc:$1}$2 [R=301,L]

EDIT2: Since i got no more feedback on this, i went and tried some other methods, and i ended up with this:

RewriteCond %{REQUEST_URI} !^/[A-Za-z]+/
RewriteCond %{REQUEST_URI} !^/[^a-z]+/
RewriteRule ^(.*)$ $1/ [R=301]
RewriteCond %{REQUEST_URI} ^/[A-Za-z]+
RewriteCond %{REQUEST_URI} ^/[^a-z]+
RewriteRule ^(.*) ${lc:$1} [R=301]
RewriteCond %{QUERY_STRING} "ReturnUrl="
RewriteRule ^(.*) $1? [R=301]

This does all i want except lowercase an uppercase URI when it doesn't start with an uppercase. So a /APp uri will get rewritten to /app but /aPp won't be. I'll just leave this here, hopefully i'll get some feedback on this.
One problem now is that sometimes the loading takes >15 seconds. And i can only relate this to the rewrite having, in the worst case, 3 301 redirects...

find the correct regex for the cond to catch only the app context

this regex will grab everything after "app/":

(?<=app/)[\S]+

now each whole match can simply be fed to a "toLowerCase()" function equivalent. regex demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM