Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I usually disallow subdirectories in the robots.txt file and I was wondering if it's possible to do it the other way: block everything and list the main index file and the others pages I'd like search engines to index. Is that possible?

My current robots.txt is as follows:

User-agent: *
Disallow: /example/
Disallow: /example/
Disallow: /example/
Disallow: /example/
Disallow: /example/
Disallow: /example/
Disallow: /example/
Disallow: yea.html
Allow: /
share|improve this question
2  
Allow: / is assumed so, in your example, you could achieve the same effect with only the Disallow directives. –  danlefree Jun 15 '11 at 7:38
add comment

1 Answer 1

up vote 6 down vote accepted

Yes. You can disallow everything first, then allow the folders you want to be indexed.

User-agent: *
Disallow: /
Allow: /index.html
Allow: /example/
Allow: /example2/
Allow: /example3/

The reason this works is that Google (and Bing) follow CSS-style specificity rules when reading robots.txt files, where more specific rules take precedence over less specific ones. From Google's robots.txt documentation:

"...for allow and disallow directives, the most specific rule based on the length of the [path] entry will trump the less specific (shorter) rule. The order of precedence for rules with wildcards is undefined."

They give a table of examples on that page. Note that the order of the rules makes no difference to how they're interpreted; only the length of them matters. You could put the 'Disallow' rule in my example above at the end of the file and it would still work as intended.

Don't forget to test your robots.txt file using Google Webmaster Tools:

To test a site's robots.txt file:

  1. On the Webmaster Tools Home page, click the site you want.
  2. Under Site configuration, click Crawler access
  3. If it's not already selected, click the Test robots.txt tab.
  4. Copy the content of your robots.txt file, and paste it into the first box.
  5. In the URLs box, list the site to test against.
  6. In the User-agents list, select the user-agents you want.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.