Apache mod重写仅使用htaccess将所有URL更改为小写

时间:2017-10-17 18:18:40

标签: regex apache .htaccess mod-rewrite

我们目前处于共享托管状态,无法访问apache global / vhost配置,因此我们被迫使用.htaccess作为可行的解决方案,我们使用了以下规则但在某些情况下它们会导致

RewriteRule '^([^Y]*)Y(.*)$' and URI '/something/tYSt/' exceeded maximum length (16380)

任何提示如何克服这个问题?

  RewriteEngine on
  RewriteBase /

  DirectoryIndex index.php index.html index.htm
  ErrorDocument 404 /index.php
  Options -Indexes
  Options +FollowSymLinks

  RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]
  RewriteRule ![A-Z] - [S=28]
  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
  RewriteRule [A-Z] - [N]
  RewriteCond %{ENV:HASCAPS} TRUE
  RewriteRule ^/?(.*) /$1 [R=301,L]

2 个答案:

答案 0 :(得分:2)

我为你找到了解决方案。只需将其放在文档根.htaccess目录中的/中:

DirectoryIndex index.php index.html index.htm
Options -Indexes +FollowSymLinks

RewriteEngine On
RewriteBase /

RewriteRule ^/?index\.php$ - [L]

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

RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule ^/?(.+)$ $1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.*)$ index.php [L]

您的版本的主要原因是您将原始URI附加到任何重写,因此您最终得到/something/tyst/something/tySt/something/tYSt/something/tYSt/....,这就是您超出最大长度的原因。

我用Apache 2.4.27

测试了这个结果

注意

在测试之前删除浏览器缓存。

答案 1 :(得分:0)

我使用了包括此源在内的多个源,并且运行了.htaccess,该源将大写字母重定向到小写字母而不会永远循环。

这是我的代码:

RewriteEngine On
# 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(.*)$ $1a$2 [L]
RewriteRule ^/?(.*)B(.*)$ $1b$2 [L]
RewriteRule ^/?(.*)C(.*)$ $1c$2 [L]
RewriteRule ^/?(.*)D(.*)$ $1d$2 [L]
RewriteRule ^/?(.*)E(.*)$ $1e$2 [L]
RewriteRule ^/?(.*)F(.*)$ $1f$2 [L]
RewriteRule ^/?(.*)G(.*)$ $1g$2 [L]
RewriteRule ^/?(.*)H(.*)$ $1h$2 [L]
RewriteRule ^/?(.*)I(.*)$ $1i$2 [L]
RewriteRule ^/?(.*)J(.*)$ $1j$2 [L]
RewriteRule ^/?(.*)K(.*)$ $1k$2 [L]
RewriteRule ^/?(.*)L(.*)$ $1l$2 [L]
RewriteRule ^/?(.*)M(.*)$ $1m$2 [L]
RewriteRule ^/?(.*)N(.*)$ $1n$2 [L]
RewriteRule ^/?(.*)O(.*)$ $1o$2 [L]
RewriteRule ^/?(.*)P(.*)$ $1p$2 [L]
RewriteRule ^/?(.*)Q(.*)$ $1q$2 [L]
RewriteRule ^/?(.*)R(.*)$ $1r$2 [L]
RewriteRule ^/?(.*)S(.*)$ $1s$2 [L]
RewriteRule ^/?(.*)T(.*)$ $1t$2 [L]
RewriteRule ^/?(.*)U(.*)$ $1u$2 [L]
RewriteRule ^/?(.*)V(.*)$ $1v$2 [L]
RewriteRule ^/?(.*)W(.*)$ $1w$2 [L]
RewriteRule ^/?(.*)X(.*)$ $1x$2 [L]
RewriteRule ^/?(.*)Y(.*)$ $1y$2 [L]
RewriteRule ^/?(.*)Z(.*)$ $1z$2 [L]
# 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]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress