Mod rewrite 重写规则问题-robots.txt

Mod rewrite 重写规则问题-robots.txt,mod-rewrite,Mod Rewrite,我当前的.htaccess RewriteRule ^(.+)\.txt$ /404.php [r=301,nc] 碰巧,所有.txt文件都被重定向到404.php 对我来说,代码看起来不错,但这也将重定向robots.txt文件的请求。 我只想取消阻止robots.txt文件,并保持其他.txt文件链接重定向。您可以使用以下选项之一从规则中排除robots.txt文件: 正则表达式负外观: 重写规则((?!robots)。+).txt$/404.php[r=301,nc] 负重写条件 重写c

我当前的.htaccess

RewriteRule ^(.+)\.txt$ /404.php [r=301,nc]
碰巧,所有
.txt
文件都被重定向到
404.php

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

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

  • 正则表达式负外观:

    重写规则((?!robots)。+).txt$/404.php[r=301,nc]

  • 负重写条件

    重写cond%{REQUEST_URI}/robots.txt$ 重写规则^(+).txt$/404.php[r=301,nc]

  • 跳过/robots.txt

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


  • 使用以下规则集

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

    您在htaccess文件中还有其他规则吗?无论如何,我已经改变了旗帜。请用这个再试一次。我在localhost中测试了它,它工作得很好。