apache从非www重定向到www [英] apache redirect from non www to www

查看:29
本文介绍了apache从非www重定向到www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站似乎无法从非 www 重定向到 www.

I have a website that doesn't seem to redirect from non-www to www.

我的Apache配置如下:

RewriteEngine On
### re-direct to www
RewriteCond %{http_host} !^www.example.com [nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc] 

我错过了什么?

推荐答案

使用重写引擎是解决这个问题的重量级方法.这是一个更简单的解决方案:

Using the rewrite engine is a pretty heavyweight way to solve this problem. Here is a simpler solution:

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / http://www.example.com/
</VirtualHost>

<VirtualHost *:80>
    ServerName www.example.com
    # real server configuration
</VirtualHost>

然后你会有另一个 部分和 ServerName www.example.com 用于您的真实服务器配置.使用 时,Apache 会自动保留 / 之后的任何内容Redirect 指令,这是关于为什么此方法不起作用(实际上它起作用)的常见误解.

And then you'll have another <VirtualHost> section with ServerName www.example.com for your real server configuration. Apache automatically preserves anything after the / when using the Redirect directive, which is a common misconception about why this method won't work (when in fact it does).

这篇关于apache从非www重定向到www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆