Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mod-Rewrite or PHP router?

Tags:

php

routing

I am debating routing my requests with one of the two options:

Option 1: simple capture route with Mod-Rewrite and funnel written $_GET route to index.php for loading...

#default routing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule    ^blog/([0-9]+)?$    index.php?rt=blog&params=$1    [L,QSA]
// ..more custom routes, and then a default route
RewriteRule    ^([A-Za-z]+)/([A-Za-z]+)/(.*)?$    index.php?rt=$1/$2&params=$3    [L,QSA]

Option 2: simply route requests to Front Controller, and create a PHP routing class to handle the routing...

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

/* --- on front controller, process $_GET['rt'] --- */

at the end of the day, which will run faster, be easier to secure, and be easier to maintain?

any other ideas?

NOTE: I am not running a known framework. I am building my own MVC pattern to learn it.

like image 733
johnnietheblack Avatar asked Dec 08 '09 16:12

johnnietheblack


People also ask

What does Mod rewrite do?

The mod_rewrite module uses a rule-based rewriting engine, based on a PCRE regular-expression parser, to rewrite requested URLs on the fly. By default, mod_rewrite maps a URL to a filesystem path. However, it can also be used to redirect one URL to another URL, or to invoke an internal proxy fetch.

What does a php router do?

Using a routing system allows us to structure our application in a better way instead of designating each request to a file. A routing system works by mapping an HTTP request to a request handler based on the request method and path specified in the URL of the request.

What is rewrite rule in Apache?

RewriteRule specifies the directive. pattern is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser. substitution is the path to the actual URL, i.e. the path of the file Apache servers. flags are optional parameters that can modify how the rule works.

What is rewrite in web server?

With a web-server rewrite, existing URLs, or URL structures, are rewritten by rules configured into a web server, without the visitor noticing much. It can be used to map internal architecture into a different client-facing architecture. The re-write process can include scripts, and the passing of an http status-code.


1 Answers

Usually in MVC frameworks, this sort of thing is usually best handled by a front controller (named index.php or the like). You use mod_rewrite to then hide index.php from all of the URLs so your users see nice clean paths.

It's also way easier to handle in PHP than in Apache's rewrite directives. PHP is much more flexible and easier to write/understand. I'm not sure I've ever seen mod_rewrite used as the sole routing engine for any web framework out there, now that I think of it.

Your second snip of code is the way to go for your rewrite directives.

like image 93
Marc W Avatar answered Sep 23 '22 00:09

Marc W