如何在目录路径更改后将部分URL从大写字母更改为小写字母 Apache / httpd



我想简短而甜蜜。我的网站地址是这个 https://www.example.com/ABCD/index.php

为了方便用户,我将 ABCD 目录更改为 abcd(小写(。现在用户键入 https://www.example.com/abcd/index.php

但是,如果用户从谷歌访问我的网站,它仍然被缓存为 https://www.example.com/ABCD/index.php 因此他们无法登录。

搜索了谷歌和堆栈溢出,我发现最接近的有效是[这里].1,即

在 http.conf 中

<IfModule mod_rewrite.c>
RewriteMap lc int:tolower
</IfModule>

和 .htaccess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(.*)$ ${lc:$1} [R=301,L]

但是,就我而言,它也不起作用。 因为它将用户从 https://www.example.com/ABCD/index.php?gene=MYH7
https://www.example.com/var/www/html/example.com/?gene=MYH7

任何有经验的人都可以帮我纠正这个规则。我想要的只是将用户引导到 https://www.example.com/abcd/index.php 如果他们键入
https://www.example.com/ABCD/index.php

这是我的网络根目录中的 .htaccess。

RewriteEngine On
# Added by Mian to direct all traffic to https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^ https://www.example.org%{REQUEST_URI} [NE,L,R]
# Added by Mian for url capital to small.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(.*)$ ${lc:$1} [R=301,L]

它似乎在 .htaccess 中不起作用。 我在我的 Apache 配置中工作了以下内容:

<VirtualHost *:80>
 rewriteengine on
 RewriteMap lc int:tolower
 RewriteRule "^/([A-Z]*)/(.*)$"  "${lc:$1}/$2"  [R=301]
</VirtualHost>

只要有一个大写字符,以下规则将 URL 的第一部分小写。

RewriteRule "^/([^/]*[A-Z][^/]*)/(.*)$"  "${lc:$1}/$2"  [R=301]

用这个改变你的规则:

RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^ ${lc:%{REQUEST_URI}} [R=301,L,NE]

变化:

  • 重定向路径以尾部斜杠开头。如果缺少尾部斜杠,则 Apache 会附加DocumentRoot路径,就像您在重定向规则中看到的那样。
  • 避免从RewriteRule中捕获值,因为您的 .htaccess 可能位于子目录中。
  • %{REQUEST_URI}表示站点根目录的完整路径

确保使用新的浏览器进行测试。

最新更新