使用 httpd.conf 将 www 重定向到非 www 的 301 语法 [英] Syntax for 301 redirect of www to non-www using httpd.conf

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

问题描述

如何设置规则(在 httpd.conf 上)将所有流量从 www.domain.com 重定向到 domain.com?以下会起作用吗?

How do I setup a rule (on httpd.conf) to redirect all the traffic from www.domain.com to domain.com? Will the following work?

<VirtualHost www.domain.com>
      Redirect 301 / http://domain.com/
</VirtualHost>

我应该把这个标签放在httpd.conf的什么地方?顺序重要吗?

Where should I put this tag in httpd.conf? Does the order matter?

推荐答案

这是您可以使用/执行的操作的示例:

This is an example of what you can use/do:

<VirtualHost *:80>

  DocumentRoot "/var/www/domain.com"
  ServerName domain.com
  ServerAlias domain.com www.domain.com

  <Directory "/path/to/public_html">
      allow from all
      Options +Indexes
  </Directory>

  Options +FollowSymLinks
  RewriteEngine On
  RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
  RewriteRule (.*) http://domain.com%{REQUEST_URI} [R=301,L]

</VirtualHost>

重定向 301 很好用,但没有重写规则那么灵活.

The Redirect 301 is good to use but does not have the same flexibility as a rewrite rule.

这篇关于使用 httpd.conf 将 www 重定向到非 www 的 301 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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