如果我不允许使用父文件夹,如何在robots.txt中允许js和css文件和图像

use*_*397 2 html javascript css robots.txt joomla1.5

在谷歌(https://www.google.com/webmasters/tools/mobile-friendly/)的移动友好网站测试工具上,它表示我的网站没有针对移动设备进行优化,但确实如此.原因是Robots.txt阻止了大量资源.我的网站基于joomla 1.5,但它有一个响应式模板.

这是我的robots.txt文件,但似乎js,css和图像仍然被阻止.

User-agent: *

Allow: /templates/
Allow: /*.js
Allow: /*.css
Allow: /*.jpg
Allow: /*.gif
Allow: /*.png

Disallow: /administrator/
Disallow: /cache/
Disallow: /components/
Disallow: /images/
Disallow: /includes/
Disallow: /installation/
Disallow: /language/
Disallow: /libraries/
Disallow: /media/
Disallow: /modules/
Disallow: /plugins/
Disallow: /tmp/
Disallow: /xmlrpc/
Disallow: /AnexosEmpresas/
Disallow: /Formulario/
Disallow: /estadisticas/
Disallow: /installation-xx/
Disallow: /site2/
Disallow: /TemplateMail/
Disallow: /IMPLEMENTACION/
Disallow: /clicks/
Disallow: /LiveZilla/
Disallow: /*format=feed*
Disallow: /*view=category*
Disallow: /*index.php/*
Disallow: /*option=com_sobi2*
Disallow: /*content/category/*
Disallow: /*start=/*
Disallow: /presentacion_ant/
Disallow: /presentacion/
Disallow: /CronJobs/
Disallow: /plantillas/
Run Code Online (Sandbox Code Playgroud)

关于如何解锁所需资源的任何想法?

pla*_*ect 7

之所以发生这种情况,是因为Google会根据路径的长度优先考虑允许和禁止竞争.具有较长路径的指令获胜.如果它们的长度相同,则允许胜过Disallow.此规则仅适用于Google.并非所有抓取工具都是这样做的.

例如,在以下内容中:

User-agent: *
Allow: /a
Disallow: /aa
Run Code Online (Sandbox Code Playgroud)

/ aardvark将被阻止(对于Google),因为"/ aa"比"/ a"长,因此Disallow优先于Allow.

在:

User-agent: *
Allow: /aa
Disallow: /a
Run Code Online (Sandbox Code Playgroud)

/ aardvark不会被阻止,因为Allow有更长的路径.

出于此规则的目的,通配符仅计为一个字符.例如,在这:

User-agent: *
Allow: /a*
Disallow: /aa
Run Code Online (Sandbox Code Playgroud)

/ aardvark 不会被阻止,因为"/ a*"与"/ aa"的长度相同(即使"/ a*"在功能上与"/ a"相同,也就是更短).

怎么解决?

选项1:

最简单的方法是简单地删除一些Disallows并接受Google将抓取您不希望它们访问的某些文件.这可能就是我要做的.这显然是一种妥协,但它是唯一能让您的robots.txt文件更易于阅读的选项.

选项2:

明确允许每个可能包含该类型文件的目录的每种文件类型.例如,这一行:

Disallow: /plugins/
Run Code Online (Sandbox Code Playgroud)

会成为这样的:

Allow: /images/*.jpg
Allow: /plugins/*.js
Allow: /plugins/*.css
Allow: /plugins/*.gif
Allow: /plugins/*.png
Disallow: /plugins/
Run Code Online (Sandbox Code Playgroud)

以上示例将阻止/ plugins /中的任何文件,除非 URL包含".jpg",".js",".css"等之一.

它会阻止:

http://example.com/plugins/
http://example.com/plugins/somefile.php
http://example.com/plugins/some/path/somefile.php
Run Code Online (Sandbox Code Playgroud)

它不会阻止:

http://example.com/plugins/somefile.js
http://example.com/plugins/somefile.jpg
http://example.com/plugins/somefile.css
http://example.com/plugins/whatever.php?file=foo.css
Run Code Online (Sandbox Code Playgroud)

您必须为要阻止的每个目录单独执行此操作.

选项3:

警告:以下是黑客攻击.我已经确认这是有效的,但它依赖于谷歌未来可能会改变的无证行为.它几乎肯定不适用于谷歌以外的爬虫.

您可以使用多个尾随通配符填充Allow,使其长度超过最长的Disallow:

Allow: /*.js***************
Allow: /*.css**************
Allow: /*.jpg**************
Allow: /*.gif**************
Allow: /*.png**************

# Your existing disallows go here.
Run Code Online (Sandbox Code Playgroud)

这些将覆盖路径不超过20个字符的任何Disallow.尾随通配符对匹配的内容没有影响.他们只增加优先权.