代码之家  ›  专栏  ›  技术社区  ›  Tallboy

使用robots.txt以外的虚拟主机重定向

  •  2
  • Tallboy  · 技术社区  · 12 年前

    我遇到了一个复杂的seo问题,谷歌从我的一个同名服务器上索引了数千个页面。我需要重定向除robots.txt以外的所有请求301

    这就是我目前所拥有的,但并不奏效。注释掉的部分是我最初放的(有效的)部分,只是它没有考虑robots.txt。下面的两行是我的失败尝试

    <VirtualHost xx.xx.xx.xx:80>
       ServerName ns2.example.com
       #RedirectMatch permanent /(.*) http://example.new/$1
       RewriteCond %{REQUEST_URI} !^/robots\.txt [NC] 
       RewriteRule ^(.*)$ http://example.new/$1 [R=301,L]
    </VirtualHost>   
    

    有人看到我的错误了吗?example.new是我要重定向到的网站

    1 回复  |  直到 12 年前
        1
  •  1
  •   Jon Lin    12 年前

    有人看到我的错误了吗?example.new是我要重定向到的网站

    对于初学者来说,您似乎需要打开重写引擎:

    RewriteEngine On
    

    这起到了作用,只是现在它将所有内容重定向为 http://example.com//whatever-page 有两个 //

    此行:

    RewriteRule ^(.*)$ http://example.new/$1 [R=301,L]
    

    需要有 / 已删除:

    RewriteRule ^(.*)$ http://example.new$1 [R=301,L]
    

    这是因为 RewriteRule is matching against在vhost/server配置中有一个前导斜杠,因此您不需要在目标中的主机名后面有斜杠。