我用的是php slim框架,url rewrite 这里是我的主机真实文件结构:
像大多数框架一样,全部重写为 index.php
/.htaccess
/assets
/index.php
/phpinfo.php
/robots.txt
/usr
/controller
/model
...
这是我的路由器
$app->get('/backstage', $authenticate(), function () use ($uri, $app) {...});
$app->get('/api', $authenticate(), function () use ($uri, $app) {...});
$app->get('/', function () use ($uri, $app) {...});
$app->get('/article', function () use ($uri, $app) {...});
如何在我的路由中禁用/backstage
、/api
,以及真实文件路径/phpin.php
、/usr
,
并在路由器中接受/
、/article
?
我很困惑应该填写路由器路径还是真实文件路径?因为真正的文件路径不存在 /article
这是我试过的
User-agent: *
Disallow: /backstage/
Disallow: /phpinfo.php
最佳答案
首先(假设您使用apache),您需要确保您的.htaccess 文件正确地将请求指向您的路由器文件。
--- 开始 .htaccess 片段 ---
<IfModule mod_rewrite.c>
RewriteEngine On
## direct all requests to Slim router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ slim-router.php [QSA,L]
</IfModule>
--- 结束.htaccess 片段---
我可能没有正确理解您的问题,但要禁用路由,只需注释掉 slim-router.php 中添加路由的行即可。
将条目添加到 robots.txt 不会阻止浏览器访问 URI,它的存在只是为了请求搜索引擎机器人(即 GoogleBot ) 不编制索引那个特定的 URI。参见 robotstxt.org和 the robots.txt entry on Wikipedia .
要将路由定向到现有文件,您可以使用 \Slim\View
类(参见 the \Slim\View
documentation )。
此示例需要一个名为 templates/article.php 的文件存在,该文件将为 /article
路由输出内容。
使用 \Slim\View
类,您还可以将数据发送到模板文件,我在下面也对此进行了演示。这只是一个基本示例,请参阅文档了解更复杂的用法。
//--- 开始 slim-router.php ---
$app = new \Slim\Slim();
$defview = new \Slim\View();
$defview->setTemplatesDirectory(realpath(__DIR__).'/templates');
$app->get(
'/article',
function () use ($app) {
global $defview;
//show the contents of 'templates/article.php', passing optional data to the template file:
$app->view($defview)->display(
'article.php',
array(
'data_one'=>'one',
'data_two'=>2,
'three'=>'3',
)
);
}
);
//--- 结束 slim-router.php ---
关于php - url 重写和谷歌机器人 txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876812/