2014-06-05 83 views
1

我對感興趣但不包括search engines有些文檔被索引,所以我用X-Robots-Tag來實現它。但是,我需要有一個正則表達式來選擇特定direcotory內的文檔,即我的案例中的secret document正則表達式幫助htaccess

對於實例

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

正如你所看到的,有可能是任何數量的左邊的目錄,但如果是「祕密文件」的最後一個目錄,我想它使用下面的代碼,以禁止htaccess

正則表達式

正則表達式應該符合以下

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

回答

1

您可以使用:

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

感謝@anubhava,你介意解釋一下你的表達式的作用嗎? –

+1

是的。 'secret \ documents /.+?\。pdf $'匹配字面文本'祕密文件/',後跟1個或多個長度字符(pdf文件名),後面是文字'.pdf'。 – anubhava

+1

''secret \ documents/\ w + \。pdf $「'也可以工作,但它不會匹配''secret文件/ some-file.pdf $」'這樣的文件名,因爲'\ w'不匹配連字符。 – anubhava

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

不要忘了逃跑後的文件斜槓。

+0

在大多數Apache正則表達式中,正斜槓並不需要轉義。 – anubhava

+0

啊,我的道歉! – Aaron