We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Which is better in term of performance .htaccess or routes.php?

I wonder should I rewrite URL using a .htaccess file or routes.php to get best performance. It also lead me to 2 different ways to get params.

.htaccess way:

<IfModule mod_rewrite.c> RewriteEngine On

RewriteRule ^hello/(.*)$ index/hello?name=$1 [QSA,L]

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

</IfModule>

In controller:

$name = $this->request->get('name', 'string');

routes.php way:

<?php

/*

  • Define custom routes. File gets included in the router service definition. */

$router = new Phalcon\Mvc\Router();

$router->add('/hello/{name}', array( 'controller' => 'index', 'action' => 'hello' ));

return $router;

In controller:

$name = $this->dispatcher->getParam('name', 'string');

Is there any hidden cons if I choose to rewrite url by using .htaccess?

Thank you in advance!



125.8k
Accepted
answer

.htaccess is technically faster. However, this is one of those things that unless every other aspect of your application is as efficient as it can be, then you're micro-optimizing in the wrong place. Unless your site gets millions of hits a day, just do what's easier. My guess would be it would be easier to add a route to your config file.