如果 URL 至少包含一个大写字母,Apache 会将所有 URL 重写为小写字母

如果 URL 至少包含一个大写字母,Apache 会将所有 URL 重写为小写字母

我一直在尝试让 Apache 将所有包含大写字符的 URL 重写为小写。出于某种原因,我找到的所有解决方案都不适用于我的情况。我希望这里有人能帮助我找出原因。

我希望如果有人点击我们页面上的 URL,http://example.com/products/productID/1234 他们就会被转到 类似http://example.com/products/productid/1234 页面。请注意,ID 是小写的。这并不总是正确的ID,我希望任何大写字符都转换为小写。

基本上我访问的任何 URL 都不会发生任何变化,这让我相信它甚至没有尝试重写它们。我已经验证了rewrite_module也未注释。

我已尝试过这里的答案:使用 .htaccess 将大写的 URL 转换为小写的并重定向

原始 httpd.conf 摘录

<VirtualHost *:80>
  ServerName dev.xxxxxxx.com
  DirectoryIndex default.cfm index.cfm index.htm index.html
  DocumentRoot "Z:/XXXXX"
  ServerAdmin [email protected]

  Redirect 301 /xxxxxxxxx/photoFiles https://www.xxxxxxxxx.com/xxxxxxxx/photoFiles
  ProxyPreserveHost On
  # ProxyPassMatch .*\.(jpg|png|gif|css|js|ico|htm|txt|csv|html|pdf|doc|docx|xls|xlsx)$ !
  # ProxyPass / balancer://nodes/ stickysession=JSESSIONID|jsessionid scolonpathdelim=on
  ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ ajp://dev.xxxxxxxx.com:7009/$1$2
  # ErrorDocument 404 /errors/404.cfm
</VirtualHost>

更新的(不起作用的)httpd.conf 摘录

<VirtualHost *:80>
  ServerName dev.xxxxxxx.com
  DirectoryIndex default.cfm index.cfm index.htm index.html
  DocumentRoot "Z:/XXXXX"
  ServerAdmin [email protected]

  RewriteEngine On
  RewriteMap  lc int:tolower
  RewriteCond %{REQUEST_URI} [A-Z]
  RewriteRule (.*) ${lc:$1} [R=301,L]

  Redirect 301 /xxxxxxxxx/photoFiles https://www.xxxxxxxxx.com/xxxxxxxx/photoFiles
  ProxyPreserveHost On
  # ProxyPassMatch .*\.(jpg|png|gif|css|js|ico|htm|txt|csv|html|pdf|doc|docx|xls|xlsx)$ !
  # ProxyPass / balancer://nodes/ stickysession=JSESSIONID|jsessionid scolonpathdelim=on
  ProxyPassMatch ^/(.+\.cf[cm])(/.*)?$ ajp://dev.xxxxxxxx.com:7009/$1$2
  # ErrorDocument 404 /errors/404.cfm
</VirtualHost>

答案1

有多种方法可以实现此目的:

httpd.conf(不在.htaccess文件中):

RewriteEngine on
RewriteBase /
RewriteMap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/?(.*)$ /${lowercase:$1} [R=301,L]

或者.htaccess(YMMV 与性能)-请注意,S=28跳过以下 28 条规则的方法(根据Apache 文档):

RewriteEngine On
RewriteBase /

# If there are caps, set HASCAPS to true and skip next rule
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]

# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]

# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*)$ $1a$2
RewriteRule ^([^B]*)B(.*)$ $1b$2
RewriteRule ^([^C]*)C(.*)$ $1c$2
RewriteRule ^([^D]*)D(.*)$ $1d$2
RewriteRule ^([^E]*)E(.*)$ $1e$2
RewriteRule ^([^F]*)F(.*)$ $1f$2
RewriteRule ^([^G]*)G(.*)$ $1g$2
RewriteRule ^([^H]*)H(.*)$ $1h$2
RewriteRule ^([^I]*)I(.*)$ $1i$2
RewriteRule ^([^J]*)J(.*)$ $1j$2
RewriteRule ^([^K]*)K(.*)$ $1k$2
RewriteRule ^([^L]*)L(.*)$ $1l$2
RewriteRule ^([^M]*)M(.*)$ $1m$2
RewriteRule ^([^N]*)N(.*)$ $1n$2
RewriteRule ^([^O]*)O(.*)$ $1o$2
RewriteRule ^([^P]*)P(.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*)$ $1q$2
RewriteRule ^([^R]*)R(.*)$ $1r$2
RewriteRule ^([^S]*)S(.*)$ $1s$2
RewriteRule ^([^T]*)T(.*)$ $1t$2
RewriteRule ^([^U]*)U(.*)$ $1u$2
RewriteRule ^([^V]*)V(.*)$ $1v$2
RewriteRule ^([^W]*)W(.*)$ $1w$2
RewriteRule ^([^X]*)X(.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*)$ $1z$2

# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule [A-Z] - [N]

RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^/?(.*) /$1 [R=301,L]

或者使用mod_spelling(您的配置表明您正在使用 Windows,因此这里也存在 YMMV):

<IfModule mod_speling.c>
  CheckCaseOnly On
  CheckSpelling On
</IfModule>

来源:http://www.askapache.com/htaccess/rewrite-uppercase-lowercase.html

答案2

如果ProxyPass是问题,您可以尝试该RewiteMap方法,但RewriteRule使用直接将发送到代理[P] flag,例如类似以下内容:

RewriteEngine on
RewriteBase /
RewriteMap lowercase int:tolower
RewriteCond $2 [A-Z]
RewriteRule ^/(.+\.cf[cm])(/.*)?$ ajp://dev.xxxxxxxx.com:7009/$1${lowercase:$2} [P]

相关内容