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

时间:2013-10-19 08:49:41

标签: php apache .htaccess mod-rewrite

我使用RewriteEngine以及htaccess规则来从网址中删除重音和大写字母:

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]

有用的参考资料

2 个答案:

答案 0 :(得分:4)

我开始工作的方法是将.htaccess文件保存为utf-8。可以找到关于如何做到这一点的解释here for 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]

答案 1 :(得分:0)

我一直试图使它在WordPress的Bitnami安装上起作用,这要求我将htaccess文件的所有更改都放在一个名为htaccess.conf的文件中。但是,当我这样做时,我得到了这些Apache错误(此示例试图将带有重音符号的字母i替换为普通的i):

[2018年9月26日星期三16:15:47.786531] [rewrite:error] [pid 11074:tid 140324793952000] [client 51.9.252.217:51184] RewriteRule'^(。)(\ xc3 \ xad)( )$'和URI'/ blah / blah / Br \ xc3 \ xadd Hehir'超出了最大长度(16380)

我放入conf文件中的完整代码是:

<Directory "/opt/bitnami/apps/wordpress/htdocs/">
# BEGIN special characters

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

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

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

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

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

RewriteCond %{REQUEST_METHOD}  !=POST
RewriteRule ^(.*)(ø|ó|ö)(.*)$ $1o$3 [E=Redirect:1,N,R]

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

RewriteCond %{ENV:Redirect} =1
RewriteRule ^ - [R,L,E=!Redirect]

# END special characters

# 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


# Wordfence WAF

<Files ".user.ini">
<IfModule mod_authz_core.c>
    Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
    Order deny,allow
    Deny from all
</IfModule>
</Files>

# END Wordfence WAF

</Directory>

当我像往常一样在.htaccess中的另一台Web服务器上运行此解决方案时,就可以了。有人可以帮忙吗?不幸的是,我无法更改Bitnami设置...

谢谢!