2

我使用RewriteEngine以下htaccess规则从 url中删除重音字母和大写字母:

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(A|Á|á|Â|â|Æ|æ|À|à|Å|å|Ã|ã|Ä|ä)(.*?)$
RewriteRule . %1a%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(B)(.*?)$
RewriteRule . %1b%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(C|Ç|ç)(.*?)$
RewriteRule . %1c%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(D)(.*?)$
RewriteRule . %1d%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(E|É|é|Ê|ê|È|è|Ð|ð|Ë|ë)(.*?)$
RewriteRule . %1e%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(F)(.*?)$
RewriteRule . %1f%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(G)(.*?)$
RewriteRule . %1g%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(H)(.*?)$
RewriteRule . %1h%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(I|Í|í|Î|î|Ì|ì|Ï|ï)(.*?)$
RewriteRule . %1i%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(J)(.*?)$
RewriteRule . %1j%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(K)(.*?)$
RewriteRule . %1k%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(L)(.*?)$
RewriteRule . %1l%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(M)(.*?)$
RewriteRule . %1m%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(N|Ñ|ñ)(.*?)$
RewriteRule . %1n%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(O|Ó|ó|Ô|ô|Œ|œ|Ò|ò|Ø|ø|Õ|õ|Ö|ö)(.*?)$
RewriteRule . %1o%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(P|ß|Þ|þ)(.*?)$
RewriteRule . %1p%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(Q)(.*?)$
RewriteRule . %1q%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(R)(.*?)$
RewriteRule . %1r%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(S)(.*?)$
RewriteRule . %1s3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(T)(.*?)$
RewriteRule . %1t%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(U|Ú|ú|Û|û|Ù|ù|Ü|ü)(.*?)$
RewriteRule . %1u%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(V)(.*?)$
RewriteRule . %1v%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(W)(.*?)$
RewriteRule . %1w%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(X)(.*?)$
RewriteRule . %1x%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(Y|Ý|ý|Ÿ|ÿ)(.*?)$
RewriteRule . %1y%3 [R=301,L]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(Z)(.*?)$
RewriteRule . %1z%3 [R=301,L]

如何使用RewriteMapmod_speling 按照本参考中的建议改进或缩短此代码:http ://www.askapache.com/htaccess/rewrite-uppercase-lowercase.html

更新答案 根据 Samurai8 的回答,我设置了一个环境变量,以便首先准备所有更改并在最后重定向。这是htaccess另存为UTF-8

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(A|Á|á|Â|â|Æ|æ|À|à|Å|å|Ã|ã|Ä|ä)(.*)$ $1a$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(B)(.*)$ $1b$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(C|Ç|ç)(.*)$ $1c$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(C|Ç|ç)(.*)$ $1c$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(D)(.*)$ $1d$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(E|É|é|Ê|ê|È|è|Ð|ð|Ë|ë)(.*)$ $1e$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(F)(.*)$ $1f$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(G)(.*)$ $1g$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(H)(.*)$ $1h$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(I|Í|í|Î|î|Ì|ì|Ï|ï)(.*)$ $1i$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(J)(.*)$ $1j$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(K)(.*)$ $1k$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(L)(.*)$ $1l$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(M)(.*)$ $1m$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(N|Ñ|ñ)(.*)$ $1n$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(O|Ó|ó|Ô|ô|Œ|œ|Ò|ò|Ø|ø|Õ|õ|Ö|ö)(.*)$ $1o$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(P|ß|Þ|þ)(.*)$ $1p$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(Q)(.*)$ $1q$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(R)(.*)$ $1r$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(S)(.*)$ $1s$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(T)(.*)$ $1t$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(U|Ú|ú|Û|û|Ù|ù|Ü|ü)(.*)$ $1u$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(V)(.*)$ $1v$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(W)(.*)$ $1w$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(X)(.*)$ $1x$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(Y)(.*)$ $1y$3 [E=Redirect:1,N,R=301]
RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(Z)(.*)$ $1z$3 [E=Redirect:1,N,R=301]
RewriteCond %{ENV:Redirect} =1
RewriteRule ^ - [R,L,E=!Redirect]

有用的参考资料

4

1 回答 1

4

我让它工作的方法是将.htaccess文件保存为 utf-8。可以在此处为 vim找到有关如何执行此操作的说明。如果您使用其他编辑器,则需要进行一些搜索。如果.htaccess文件不是保存为 utf-8 文件,而是保存为 ascii 文件,则 RewriteRules 将留下“奇怪的字符”。localhost/à将被重写localhost/a¡为例如。我只能猜测其原因,但我认为这是因为 Apache 假定字符串具有与实际不同的字符编码。

如果一切都按预期工作,您可以更改[R][R=301]使重定向永久化。永久重定向由浏览器缓存,减少服务器对未来请求的负载,但如果您仍在测试,它们会弄乱重写,因为可以缓存旧的重写。

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(Á|á|Â|â|Æ|æ|À|à|Å|å|Ã|ã|Ä|ä|A)(.*)$ $1a$3 [E=Redirect:1,N,R]

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(B)(.*)$ $1b$3 [E=Redirect:1,N,R]

#etc...

RewriteCond %{ENV:Redirect} =1
RewriteRule ^ - [R,L,E=!Redirect]
于 2013-10-19T10:48:43.077 回答