正则表达式有助于htaccess

时间:2014-06-05 14:48:19

标签: regex apache .htaccess search-engine

我对排除某些文档在search engines上编入索引感兴趣,因此我使用X-Robots-Tag来实现它。但是,我需要一个正则表达式来选择特定目录中的文档,例如secret document

例如

    /dir1/dir2/secret documents/file1.pdf
    /dira/dir1/dir2/secret documents/file2.pdf
    /dir1a/secret documents/file3.pdf
    /dir1a/other documents/file4.pdf

如您所见,左侧可能有任意数量的目录,但最后一个目录是“秘密文档”,我想在htaccess中使用以下代码禁止它。

正则表达式

RegEx应该在下面

<Files ~ "\.pdf$">
  Header set X-Robots-Tag "noindex, nofollow"
</Files>

2 个答案:

答案 0 :(得分:1)

您可以使用:

<Files ~ "secret\ documents/.+?\.pdf$">
  Header set X-Robots-Tag "noindex, nofollow"
</Files>

答案 1 :(得分:1)

<Files ~ "secret\ documents\/.+?\.pdf$">
  Header set X-Robots-Tag "noindex, nofollow"
</Files>

不要忘记在文件之后逃避正斜杠。