我想确保用户来到www.mydomain.com
即使他们通过别名mydomain.com
到达。 这样我就可以控制子域上的Cookie,这样Google就可以看到一个域名,而不是一个url的大杂烩。
我怎样才能做到这一点与Apache?
<VirtualHost ip:80> ServerName domain.com RedirectMatch permanent ^(.*)$ http://www.domain.com$1 </VirtualHost> <VirtualHost ip:80> ServerName www.domain.com ... usual config </VirtualHost>
把这个添加到你的httpd.conf
文件中:
Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC] RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
启用mod_rewrite支持,然后在您的域的根文件夹中为以下内容创build一个.htaccess文件:
<IfModule mod_rewrite.c> Options +FollowSymLinks Options +Indexes RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^domain\.com$ RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L] </IfModule>
如果您确实希望Google使用特定的域名风格(使用或不使用www ),请创build一个免费的Google网站pipe理员工具帐户,validation您的域的所有权(上传文件),并使用在线控制面板设置您的首选域 。
其简单,你不需要改变任何服务器端代码,有或没有www的入站链接将被视为相同的增加您的网页的PageRank。
链接可能指向您的网站使用www和非www版本的URL(例如http://www.example.com和http://example.com )。 首选的域名是您希望在search结果中用于您的网站的版本。
一旦您告诉我们您的首选域名,我们将在显示url时考虑您的偏好。 您可能需要一段时间才能看到这一变化完全反映在我们的指数中。
如果您不指定首选域名,我们可能会将域名的www和非www版本视为单独页面的单独引用。
我有相反的问题,当我只想从domain.com直接提供服务时,他们去www.domain.com:
<VirtualHost *:80> ServerName domain.com ... usual config </VirtualHost> <VirtualHost *:80> ServerName www.domain.com ServerAlias sub.domain.com # can also catch some other subdomains ServerAlias *.domain.com # or all previously unmatched domains Redirect permanent / http://domain.com/ </VirtualHost>
正如Greg Hewgill所说,使用Redirect比RedirectMatch简单一些,可以做同样的工作,详情请参阅http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirect 。