doulei2100 2013-10-19 08:49
浏览 66
已采纳

.htaccess - 如何将大写和重音字母重定向到url中的非重音小写?

I use the RewriteEngine with the following htaccess rules to remove accented and capitalized letters from urls:

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]

How can I improve or make this code shorter whitout using RewriteMap or mod_speling as suggested in this reference: http://www.askapache.com/htaccess/rewrite-uppercase-lowercase.html

UPDATE ANSWER According to Samurai8's answer I have set an environment variable in order to prepare all changes first and redirect at the end. Here is the htaccess save as 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]

HELPFUL REFERENCES

  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 在matlab中如何进行三个参数的离散傅里叶逆变换(idft)
      • ¥15 遇到问题了,求解答!
      • ¥15 请问coppliasim eduUR5视觉抓取怎么实现仿真,
      • ¥30 JavaWeb实验(购物平台)
      • ¥15 八路抢答器倒计时设计时显示器不输出,只能显示0
      • ¥15 用C语言随机生成一个迷宫
      • ¥15 超多因素的正交方案设计
      • ¥15 Scratch~汽车小游戏
      • ¥30 OSGB转换为3dtiles
      • ¥25 用于Audio的芯片中“Audio Interface”和“Mode Control”是什么?