Reputation:
Zend framework 2x how can i make http://www.example.com/robots.txt
?
i have following in bootstrap but it fails to understand the "robots.txt" only works when i go to http://www.example.com/robots
$route = Http\Segment::factory(array(
'route' => '/robots[/:action]',
'defaults' => array(
'controller' => 'Application\Controller\Sitemap',
'action' => 'index'
),
));
$router->addRoute('robots', $route, null);
Upvotes: 1
Views: 959
Reputation: 44393
1) Add robots.txt
to physical path.
You can just add a physical robots.txt
file in you public path. ZF2 will not reroute traffic to existing files in the public path.
So if you put the file inside /public/robots.txt
then a request to the following will simply load the file:
http://www.example.com/robots.txt
2) Make sure the robots.txt
has content
The file needs content otherwise it wont work.
So for example:
User-agent: *
Disallow: /
3) Check your rewrite rules
If it still doesn't work you should check whether your rewriting rules (.htaccess
or web.config
file) are configured according to the specs in the ZF2 documentation
Upvotes: 1
Reputation: 402
Add .txt
after 'route' => '/robots.txt',
and remove the [/:action]
So your code would look like this.
$route = Http\Segment::factory(array(
'route' => '/robots.txt',
'defaults' => array(
'controller' => 'Application\Controller\Sitemap',
'action' => 'index'
),
));
$router->addRoute('robots', $route, null);
Edit: I know some people would want it to be physical on disk, but this way you have a dynamicrobots.txt file which you can fill in with stuff from the database.
Edit2: And yes i know that you can write it on disk, but there's a pletora of ways you can do this.
Upvotes: 1
Reputation: 753
If you want to have link to www.example.com/robots.txt simple save this robots.txt file into public folder
Upvotes: 1