代码中的3条评论相当准确地解释了我想实现的目标。无法使htaccess重定向www到非www网站
<IfModule mod_rewrite.c>
RewriteEngine On
# Change secretdiary.org/index.php?url=URL to secretdiary.org/URL on the browser's url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
# Redirect http://www.secretdiary.org/ to http://secretdiary.org/
RewriteCond %{HTTP_HOST} !^secretdiary.org$ [NC]
RewriteRule ^(.*)$ http://secretdiary.org/$1 [L,R=301]
# Add trailing slash/if there's none
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
</IfModule>
但是,我发现一些问题,我认为他们来自条件放在一起。当我输入www.secretdiary.org/about
时,它将(在浏览器中显示)到secretdiary.org/index.php?url=about
,删除www,但忽略第一条规则。切换订单根本没有帮助,也没有与RewriteBase
混淆。但是,如果我没有www
而正常输入,则uri通常显示为secretdiary.org/about
,而不进行任何重写。 这是为什么?我该如何解决?
此外,我已经按照this answer和this other试图自动添加一个尾部的斜杠到uri如果失踪。我可以用PHP(if (substr($_GET['url'], -1) != "/") header("Location: " . htmlspecialchars($_GET['url']) . '/');
,但现在它困扰我,我不能用.htaccess实现它,所以如果你也可以发现问题在哪里,这将是非常有益的
我会把WWW。先去除。请记住,RewriteConds是正则表达式的,所以要逃避。'(\。)'。你可以进入任何情况下你添加尾随/之后**但实际的目录?你不应该那样做。只有目录应该有/。 –