我想进行重定向,以便将访问http://www.example.com
的所有用户重定向到http://example.com
。我正在使用DigitalOcean(Ubuntu 14.10 x64)。
首先,我在DigitalOcean DNS管理中创建了一个CNAME
记录:
- (Enter name) www
- (Enter hostname) @
其次,我将以下代码放在.htaccess
:
Options -Indexes
# Redirect www urls to non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
不幸的是,它不起作用。我可以访问http://www.example.com
和http://example.com
,但在访问http://www.example.com
时我无法重定向。
非常感谢任何帮助。
答案 0 :(得分:1)
有点偏离主题,但作为替代解决方案,您可以使用PHP执行此操作;像这样的东西:
if (SUBSTR($_SERVER['HTTP_HOST'], 0, 4) === 'www.') {
header('Location: http'.(ISSET($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 's':'').'://' . SUBSTR($_SERVER['HTTP_HOST'], 4).$_SERVER['REQUEST_URI']);
exit;
}