.htaccess www到非www

时间:2012-02-09 03:42:43

标签: .htaccess

我知道有很多关于此的堆栈溢出帖子。我在/ var / www /目录中有这个:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

它应该将所有www.domain.com请求重写为http://domain.com。我启用了mod_rewrite,在/ etc / apache2 / sites-available / default中,我启用了htaccess:

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

所有www请求都会导致找不到页面错误。我得到一个通用的verizon搜索。我到底做错了什么?

修改 这是整个文件/ etc / apache2 / sites-available / default

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>

重写日志文件

173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b850f3b0/initial] (2) init rewrite engine with requested uri /
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b850f3b0/initial] (1) pass through /
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b850f3b0/initial] (3) [perdir /var/www/] strip per-dir prefix: /var/www/ -> 
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b850f3b0/initial] (3) [perdir /var/www/] applying pattern '^(.*)$' to uri ''
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b850f3b0/initial] (4) [perdir /var/www/] RewriteCond: input='domain.com' pattern='^www\.(.+)$' [NC] => not-matched
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b850f3b0/initial] (1) [perdir /var/www/] pass through /var/www/
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8506390/subreq] (2) init rewrite engine with requested uri /index.html
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8506390/subreq] (1) pass through /index.html
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8506390/subreq] (1) [perdir /var/www/] pass through /var/www/index.html
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b850d3a8/subreq] (2) init rewrite engine with requested uri /index.cgi
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b850d3a8/subreq] (1) pass through /index.cgi
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b850d3a8/subreq] (1) [perdir /var/www/] pass through /var/www/index.cgi
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8506390/subreq] (2) init rewrite engine with requested uri /index.pl
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8506390/subreq] (1) pass through /index.pl
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8506390/subreq] (1) [perdir /var/www/] pass through /var/www/index.pl
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b850d3a8/subreq] (2) init rewrite engine with requested uri /index.php
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b850d3a8/subreq] (1) pass through /index.php
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b850d3a8/subreq] (1) [perdir /var/www/] pass through /var/www/index.php
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85718b0/initial] (2) init rewrite engine with requested uri /css/style.css
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85718b0/initial] (1) pass through /css/style.css
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85718b0/initial] (3) [perdir /var/www/] strip per-dir prefix: /var/www/css/style.css -> css/style.css
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85718b0/initial] (3) [perdir /var/www/] applying pattern '^(.*)$' to uri 'css/style.css'
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85718b0/initial] (4) [perdir /var/www/] RewriteCond: input='domain.com' pattern='^www\.(.+)$' [NC] => not-matched
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85718b0/initial] (1) [perdir /var/www/] pass through /var/www/css/style.css
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8554138/initial] (2) init rewrite engine with requested uri /js/scripts.js
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8554138/initial] (1) pass through /js/scripts.js
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8554138/initial] (3) [perdir /var/www/] strip per-dir prefix: /var/www/js/scripts.js -> js/scripts.js
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8554138/initial] (3) [perdir /var/www/] applying pattern '^(.*)$' to uri 'js/scripts.js'
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8554138/initial] (4) [perdir /var/www/] RewriteCond: input='domain.com' pattern='^www\.(.+)$' [NC] => not-matched
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8554138/initial] (1) [perdir /var/www/] pass through /var/www/js/scripts.js
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (2) init rewrite engine with requested uri /images/HeaderBkg.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (1) pass through /images/HeaderBkg.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (3) [perdir /var/www/] strip per-dir prefix: /var/www/images/HeaderBkg.gif -> images/HeaderBkg.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (3) [perdir /var/www/] applying pattern '^(.*)$' to uri 'images/HeaderBkg.gif'
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (4) [perdir /var/www/] RewriteCond: input='domain.com' pattern='^www\.(.+)$' [NC] => not-matched
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (1) [perdir /var/www/] pass through /var/www/images/HeaderBkg.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (2) init rewrite engine with requested uri /images/logo.png
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (1) pass through /images/logo.png
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (3) [perdir /var/www/] strip per-dir prefix: /var/www/images/logo.png -> images/logo.png
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (3) [perdir /var/www/] applying pattern '^(.*)$' to uri 'images/logo.png'
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (4) [perdir /var/www/] RewriteCond: input='domain.com' pattern='^www\.(.+)$' [NC] => not-matched
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (1) [perdir /var/www/] pass through /var/www/images/logo.png
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85718b0/initial] (2) init rewrite engine with requested uri /images/SearchHeadingIco.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85718b0/initial] (1) pass through /images/SearchHeadingIco.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85718b0/initial] (3) [perdir /var/www/] strip per-dir prefix: /var/www/images/SearchHeadingIco.gif -> images/SearchHeadingIco.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85718b0/initial] (3) [perdir /var/www/] applying pattern '^(.*)$' to uri 'images/SearchHeadingIco.gif'
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85718b0/initial] (4) [perdir /var/www/] RewriteCond: input='domain.com' pattern='^www\.(.+)$' [NC] => not-matched
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85718b0/initial] (1) [perdir /var/www/] pass through /var/www/images/SearchHeadingIco.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b854f600/initial] (2) init rewrite engine with requested uri /images/SearchKeywordBkg.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b854f600/initial] (1) pass through /images/SearchKeywordBkg.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b854f600/initial] (3) [perdir /var/www/] strip per-dir prefix: /var/www/images/SearchKeywordBkg.gif -> images/SearchKeywordBkg.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b854f600/initial] (3) [perdir /var/www/] applying pattern '^(.*)$' to uri 'images/SearchKeywordBkg.gif'
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b854f600/initial] (4) [perdir /var/www/] RewriteCond: input='domain.com' pattern='^www\.(.+)$' [NC] => not-matched
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b854f600/initial] (1) [perdir /var/www/] pass through /var/www/images/SearchKeywordBkg.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (2) init rewrite engine with requested uri /images/CategoriesHeadingIco.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (1) pass through /images/CategoriesHeadingIco.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (3) [perdir /var/www/] strip per-dir prefix: /var/www/images/CategoriesHeadingIco.gif -> images/CategoriesHeadingIco.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (3) [perdir /var/www/] applying pattern '^(.*)$' to uri 'images/CategoriesHeadingIco.gif'
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (4) [perdir /var/www/] RewriteCond: input='domain.com' pattern='^www\.(.+)$' [NC] => not-matched
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b8504388/initial] (1) [perdir /var/www/] pass through /var/www/images/CategoriesHeadingIco.gif
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85113b8/initial] (2) init rewrite engine with requested uri /images/SRightIco.png
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85113b8/initial] (1) pass through /images/SRightIco.png
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85113b8/initial] (3) [perdir /var/www/] strip per-dir prefix: /var/www/images/SRightIco.png -> images/SRightIco.png
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85113b8/initial] (3) [perdir /var/www/] applying pattern '^(.*)$' to uri 'images/SRightIco.png'
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85113b8/initial] (4) [perdir /var/www/] RewriteCond: input='domain.com' pattern='^www\.(.+)$' [NC] => not-matched
173.68.188.109 - - [10/Feb/2012:14:58:24 +0000] [domain.com/sid#b8370ad8][rid#b85113b8/initial] (1) [perdir /var/www/] pass through /var/www/images/SRightIco.png

2 个答案:

答案 0 :(得分:1)

Apache很好。我不得不调整DNS的cname记录。

对于遇到此问题的其他人,我将我的CNAME值更改为domain.com。这是www.domain.com。这是我的问题的原因。

答案 1 :(得分:0)

我没有看到servername&amp;虚拟主机中的serveralias

servername Apache Docs
serveralias Apache Docs

将它们添加到虚拟主机部分。

<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com 
.
.
.
</virtualhost>

RewriteEngine On
RewriteLog "/path/to/rewrite.log"
RewriteLogLevel 9

将以上3行放在virtualhost中。重启httpd。并访问www.domain.com 一次并将日志粘贴到此处。