Search code examples
.htaccessmod-rewritecodeigniter-2

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


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.


Solution

  • 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.