We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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]


In controller:

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

routes.php way:



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


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