重定向非www到www网址
当人们访问我的域名被重定向到使用PHP code.I添加在.htaccess下面的代码重定向非www到www网址
RewriteEngine on
Options +FollowSymlinks
RewriteBase/
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RedirectPermanent /pages/abc-123.html http://www.mydomain.com/en/page-a1/abc.php
从非WWW重定向人到www http://www.mydomain.com/en/index.php ,
不过用户可以通过输入两个http://mydomain.com/en/page-a1/abc.php和http://www.mydomain.com/en/page-a1/abc.php网址
有谁知道的方法来完全访问重定向即使用户输入了http://www.mydomain.com/en/page-a1/abc.php,也禁止访问没有www的URL。
我不知道如何通过.htaccess来做到这一点,但我使用自己的PHP代码在我的config.php
中为每个文件加载。
if(substr($_SERVER['SERVER_NAME'],0,4) != "www." && $_SERVER['SERVER_NAME'] != 'localhost')
header('Location: http://www.'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
编辑: @genesis,你是正确的我忘了HTTPS
变化
header('Location: http://www.'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
到
header('Location: '.
(@$_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://').
'www.'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
https - > http ?? – genesis
您应该使用HTTP_HOST而不是SERVER_NAME; (和strncmp()而不是substr();)) – arnaud576875
我之前使用过HTTP_HOST,我切换到了SERVER_NAME,因为它包含了最后的'/'。为什么我应该使用HTTP_HOST? – Kokos
Redirect 301 /pages/abc-123.html http://www.mydomain.com/en/page-a1/abc.php
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
# mydomain.com -> www.mydomain.com
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
</IfModule>
$protocol = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
header('Location: '.$protocol.'www.'.$_SERVER['HTTP_HOST'].'/'.$_SERVER['REQUEST_URI']);
exit;
}
在PHP
'strncmp($ _ SERVER ['HTTP_HOST'],'www。',4)=== 0';) – arnaud576875
如果我访问'sub.mydomain.com'会怎么样? –
斜杠不是必须的“/”。它会在重定向上产生不需要的附加斜线。 '$ _SERVER ['HTTP_HOST']。$ _ SERVER ['REQUEST_URI']' – sugunan
我想你想将用户重定向,而不是重写URL,在这种情况下使用Redirect
或“RedirectMatch`指令。 http://httpd.apache.org/docs/2.3/rewrite/remapping.html#old-to-new-extern
RewriteCond
之前添加RewriteEngine On
,使您的重写规则:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
如果你有HTTPS:
RewriteEngine On
RewriteRule .? - [E=PROTO:http]
RewriteCond %{HTTPS} =on
RewriteRule .? - [E=PROTO:https]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ %{ENV:PROTO}://www.%{HTTP_HOST}/$1 [R=301,L]
嗨arnaud576875,我已经添加了RewriteEngine,但仍然无效。 –
请确保在您的服务器中启用了.htaccess – arnaud576875
.htaccess已启用,因为随着上面提到的代码,很少有RedirectPermanent重定向301代码可以正常工作。 –
<?php
$protocol = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
header('Location: '.$protocol.'www.'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
exit;
}
?>
正常工作的
虽然这是一个答案,你可以扩展它吗? http://*.com/help/how-to-answer –
是重写的想法是将用户重定向到www,如果他来到非www。因此,非www仍然可以访问,但告诉浏览器重定向到www并将该url保存在缓存中,因为这是永久性的(301)重定向。所以,从用户的角度来看,当他访问'domain.com'时,浏览器会自动重定向到'www.domain.com'。你的用户在浏览器的地址栏中看到'domain.com'吗? – J0HN
不,用户不会看到domain.com,但他可以在浏览器中打开http://mydomain.com/en/page-a1/abc.php页面。 –
你想做什么?禁止用户在浏览器中输入该URL?:)如果您想要拒绝'domain.com'服务,只需将其重定向到'404'页面,例如'RewriteRule ^(。*)$ 404.php [L ]' – J0HN