有没有办法使用.htaccess将网址重定向到它的小写版本(虽然只是其中的一部分)?
http://siteaddress.com/Page/ Some-Mixed-Chars-Url 至http://siteaddress.com/Page/ some-mixed-chars-url
提前致谢。
这是我的.htaccess文件
选项-MultiViews
RewriteEngine On
RewriteBase /
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]
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
答案 0 :(得分:0)
由于您无权访问httpd.conf
文件,因此您需要让Apache将包含大写字符的请求发送到将处理重定向的PHP脚本。
<强> .htaccess
强>:
RewriteEngine on
RewriteBase /
# Check if the request contains upper-case characters:
RewriteCond %{REQUEST_URI} [A-Z]
# If so, pass it on to the script:
RewriteRule (.*) utils/tolower.php?request=$1 [QSA,L]
然后,在 utils/tolower.php
:
<?php
# If we have a request, process it
if (isset($_GET['request']))
{
$url = $_GET['request'];
unset($_GET['request']);
$params = http_build_query($_GET);
if (strlen($params)) $params = '?' . $params;
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/' . strtolower($url) . $params, true, 302);
exit();
}
# If not, throw a 404 error
header("HTTP/1.0 404 Not Found");
die("Sorry, but you didn't provide a request to convert to lower-case.");
要使重定向成为永久性,请在302
方法中将301
更改为header()
。
答案 1 :(得分:0)
我最终使用php进行此操作。
以下是Laravel的解决方案。
Route::get('/Page/{slug}, function($slug){
if(preg_match('/[A-Z]/', $slug)){
return Redirect::to('/Page/'.strtolower($slug), 301);
}
//other stuff
});