Home How to do an internal redirect with CodeIgniter for robots.txt?
Reply: 1

How to do an internal redirect with CodeIgniter for robots.txt?

Liam
1#
Liam Published in 2017-12-05 20:34:11Z

I have a server running Apache 2.2.24 and CodeIgniter 2.1.0 .

I want to handle requests for /robots.txt dynamically, using CodeIgniter, but the only solution I have found so far is to use .htaccess to issue a temporary redirect that tells clients to fetch /robots_txt instead (note the underscore).

RewriteRule ^robots\.txt$ /robots_txt [NC,L,R=307]

Later in the .htaccess there are these rules for CodeIgniter:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1

Within CodeIgniter I can route requests for robots_txt to a controller.

My question is, how can I change this to an internal redirect? That is, when I get a request for robots.txt, instead of issuing a redirect response to the client, how can I rewrite the url to handle it internally as robots_txt?

Anything I have tried leads to a 404.

anubhava
2#
anubhava Reply to 2017-12-05 20:54:47Z

To avoid a full redirect just do it as follows:

# rewrite robots.txt to index.php
RewriteRule ^robots\.txt$ index.php?/$0 [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Then within CodeIgniter you can route requests for robots.txt (no underscore here) to a controller.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.339563 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO