使用htaccess

时间:2018-07-28 17:45:17

标签: apache .htaccess redirect mod-rewrite

我想将example.com/para1/para2转换为example.com/PARA1/PARA2。我正在使用共享主机,因此只能将htaccess用于重写。我发现下面的htaccess代码将上层转换成下层,我尝试通过替换所有下层到上层以及上层到下层来使用它,但不幸的是,id无效。

RewriteEngine On
RewriteBase /

# 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]*)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

# 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]

将非常感谢您的友善回答。

1 个答案:

答案 0 :(得分:1)

upper -> lower转换为lower -> upper很简单,因为您已经完成了第一个工作。您要做的就是应用逆逻辑

RewriteEngine On
RewriteBase /

# If there are lowers, set HASLOWERS to true and skip next rule
RewriteRule [a-z] - [E=HASLOWERS:TRUE,S=1]

# Skip this entire section if no lowercase letters in requested URL
RewriteRule ![a-z] - [S=28]

# Replace single occurance of lower with LOWER, then process next Rule.
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

# If there are any lowercase letters, restart at very first RewriteRule in file.
RewriteRule [a-z] - [N]

RewriteCond %{ENV:HASLOWERS} TRUE
RewriteRule ^/?(.*) /$1 [R=301,L]