【问题标题】:RewriteRule issue - robots.txtRewriteRule 问题 - robots.txt
【发布时间】:2016-05-16 06:20:19
【问题描述】:

我当前的 .htaccess 有

RewriteRule ^(.+)\.txt$ /404.php [r=301,nc]

碰巧,所有.txt 文件都被重定向到404.php

代码对我来说看起来不错,但这也会重定向 robots.txt 文件的请求。 我只想取消阻止 robots.txt 文件并保持其他 .txt 文件链接重定向。

【问题讨论】:

    标签: mod-rewrite


    【解决方案1】:

    您可以使用以下选项之一从规则中排除 robots.txt 文件:

    1. 正则表达式负向:

      RewriteRule ((?!robots).+).txt$ /404.php [r=301,nc]

    2. 负rewriteCond

      RewriteCond %{REQUEST_URI} !/robots.txt$ RewriteRule ^(.+).txt$ /404.php [r=301,nc]

    3. 跳过 /robots.txt

      重写规则 robots.txt$ - [L] RewriteRule ^(.+).txt$ /404.php [r=301,nc]

    【讨论】:

      【解决方案2】:

      使用以下规则集

      RewriteEngine on
      RewriteCond %{REQUEST_URI} !/robots\.txt$
      RewriteRule ^(.+)\.txt$ /404.php [R=301,L]
      

      【讨论】:

      • htaccess 文件中还有其他规则吗?无论如何,我已经改变了标志。请再试一次。我在 localhost 中对其进行了测试,并且工作正常。
      猜你喜欢
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多