如何在robots.txt中禁止所有文件和文件夹,并允许其中一些?

问题描述

我需要robots.txt推荐下一个

  1. 禁止所有文件文件
  2. 只允许index.PHPmsub.PHP

我已经为msub.PHP中的RewriteReuls做过.htaccess链接,如下所示:

domain.com/p/subject122

domain.com/p/subject104

所以我想使链接显示搜索引擎中,就像:domain.com/p/subject122。

我该怎么做?

我尝试了此命令,但效果不佳。.链接domain.com/p/subject104未出现在搜索引擎中

User-agent: *
disallow: /
Allow: /index.PHP
Allow: /msub.PHP

.htaccess

Options -Indexes

RewriteEngine On
RewriteBase /

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /error.html [L]

RewriteRule home /index.PHP 
RewriteRule p/(.*)$ msub.PHP?page=$1

解决方法

robots.txt应该包含搜寻器看到的URI,无论您如何内部处理它们。因此,您的robots.txt应该具有:

User-agent: *
Disallow: /
Allow: /p/subject122
Allow: /p/subject104

您的.htaccess也可以重构:

Options -Indexes

RewriteEngine On
RewriteBase /

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]

RewriteRule ^404/?$$ error.html [L,NC]

RewriteRule ^home/?$ index.php [L,NC]

RewriteRule p/(.*)$ msub.php?page=$1 [L,NC,QSA]