【问题标题】:how to change part of the url from capital to small letters after the directory path is changed apache/httpd更改目录路径后如何将部分url从大写字母更改为小写字母apache / httpd
【发布时间】:2018-08-15 13:25:47
【问题描述】:

我想要又短又甜。我的网址是这个 https://www.example.com/ABCD/index.php

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

但是,如果用户从 google 访问我的网站,它仍然会被缓存为 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]

【问题讨论】:

  • 确实又短又甜。

标签: regex linux apache web-services


【解决方案1】:

它似乎在 .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]

【讨论】:

  • 仍然和我从上面得到的输出一样。这是我在根目录中的 .htaccess 归档存储。
    {
    RewriteEngine On
    # 由 Mian 添加,用于将所有流量导向 https
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www \.
    RewriteRule ^ example.org%{REQUEST_URI} [NE,L,R]
    #RewriteCond %{REQUEST_URI} [AZ]
    #RewriteRule ^ ${lc:%{REQUEST_URI}} [R=301,L,NE]
    重写规则 "^(.*[AZ].*)$" "${lc:$1}" [R=301,L]
    }
  • 当然,您没有在 OP 中显示第一条规则。但被处决的是那个。
  • 哦,所以它导致了这个问题。我现在该怎么办?让我把这个添加到问题中。
  • 如果你不需要它,把前 3 行扔掉。
  • 我已经尝试了一切,它在 .htaccess 中不起作用。在 apache conf 中的虚拟主机内:立即。
【解决方案2】:

用这个改变你的规则:

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

变化:

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

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

【讨论】:

  • 我保持 httpd.conf 不变,并用你在 .htaccess 中建议的两行替换了上面的四行,我得到了一个错误的路径,即 `` example.com/abcd/index.php/index.php/index.php/index.php/… ```大写字母是写一个 index.php 加上最后一个。你想让我从 httpd.conf 中删除代码并只在 .htaccess 中添加它吗?
  • 顺便说一句,我认为您是帮助我的合适人选。 Java正则表达式一书的作者:)
  • 将此规则放在任何其他规则之前 就在RewriteEngine On 行下方,并从新浏览器重新测试。此规则经过全面测试,可在我的 Apache 上运行。
  • 当我添加你提到的两行时,网站被重定向到example.org/?gene=MYH7
  • 您在浏览器中输入的原始网址是什么?您是否禁用了浏览器缓存?最后显示您最新的 .htaccess 有问题。
猜你喜欢
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
相关资源
最近更新 更多