Search code examples
regexapache.htaccesssearch-engine

Regular Expressions help for htaccess


I am interested in excluding some documents from being indexed on search engines, so I am using X-Robots-Tag to achieve it. However, I need to have a regular expression which will select documents within a specific direcotory i.e. secret document in my case.

For Example

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

As you can see, there could be any number of directories on left, but the last directory if is "secret documents", i want to disallow it using following code in htaccess.

Regular Expression

RegEx should fit in below

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

Solution

  • You can use:

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