Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
925 views
in Technique[技术] by (71.8m points)

apache - Syntax for 301 redirect of www to non-www using httpd.conf

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>

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...