.htaccess 重写UC文字strtolower

.htaccess 重写UC文字strtolower,.htaccess,mod-rewrite,url-rewriting,rewrite,.htaccess,Mod Rewrite,Url Rewriting,Rewrite,是否可以制定重写规则来执行此操作 我的php文件都有第一个大写字符,有些文件可能有连字符 喜欢 Filename.php和File-Name.php 首先 # Try this rewrite only if the file is not found RewriteCond %{REQUEST_FILENAME} !-f 然后我想为这样的请求制定一个规则 mysite.com/thing mysite.com/Thing mysite.com/THING 重写到位于的真实文件 mysite

是否可以制定重写规则来执行此操作

我的php文件都有第一个大写字符,有些文件可能有连字符
喜欢 Filename.php和File-Name.php

首先

# Try this rewrite only if the file is not found
RewriteCond %{REQUEST_FILENAME} !-f
然后我想为这样的请求制定一个规则

mysite.com/thing
mysite.com/Thing
mysite.com/THING
重写到位于的真实文件

mysite.com/Thing.php
mysite.com/Another-Thing.php
或(使用连字符)

重写到位于的真实文件

mysite.com/Thing.php
mysite.com/Another-Thing.php
然后,如果仍然找不到该文件,请放弃并将错误重新写入index.php,即

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [R=301,L]
我可以想出一种在php中实现这一点的方法,但不确定是否可以在重写中实现

$url = str_replace('-', ' ', $url);
$url = ucwords(strtolower($url));
$url = str_replace(' ', '-', $url) . ".php";

谢谢。

您可以将以下声明添加到apache或vhost配置文件中:

 RewriteMap lc int:tolower
然后在同一文件或
文件中执行以下规则。例如,htaccess
文件可以使用此映射函数对字符串进行操作

 RewriteRule ^                        -       [E=LC_URI:${lc:%{REQUEST_URI}}]
现在,您可以在重写规则中使用
%{ENV:LC_URI}
,或者如果要删除前导/:

 RewriteCond   ${lc:%{REQUEST_URI}}   ^/(.*)$
 RewriteRule   ^.*                    %1      [E=LC_FILE:%1]

等等。

您可以在重写中执行此操作,但前提是您具有对服务器配置的root访问权限——使用名为RewriteMap的指令。但是,如果您只有对重写引擎的
.htaccess
访问权限,则无法使用此选项。