Is it better to handle friendly/clean/pretty URLs with mod_rewrite or a language like PHP?
Option 1 (.htaccess and several .php files) was often used "in the past" ; now, I see option 2 (every request going through one .php file) used a lot more.
The main advantages I see with option 2 are :
- you can add / modify any kind of URL without having to change any physical file like .htaccess
- which means the format of the URLs can be configured in the admin section of your application, for example
- you only have one entry point to your PHP code.
- which means everything goes though
index.php
: if you need some code executed for all requests, put it there, and you're sure it'll always be executed. - That's used a lot with MVC frameworks, for instance.
- which means everything goes though
A couple of years ago, I would have gone with option 1 ; now that I use MVC and Frameworks, I always go with option 2.
Really this is the "are frameworks worth using?" question in disguise.
Using mod_rewrite to define your URL routes is quick and easy (if you understand regular expressions...) but your application code is oblivious to the URLs unless you duplicate the information somewhere.
Usually, people duplicate this information many times without thinking about it, by hard-coding URLs in the links in their views, or in redirects. This is really messy, and will one day cause pain when you decide to change the URL structure of your site halfway through development. You're bound to miss one and end up with a 404 somewhere.
Using a routing component in your application (such as the one in Symfony) means you can attach names to your routes, allowing you to define your URLs once and re-use them many times:
# apps/frontend/config/routing.ymlhomepage: url: / param: { module: default, action: index }
This makes it really easy to link to pages of your site without repeating yourself:
<?php echo url_for('@homepage') ?>
Use option #2 - why? RewriteRules in .htaccess
are powerful tool, but they're some kind of static. I mean you cannot easily manage then using PHP (or whatever you're going to use). Also .htaccess
doesn't provide so much flexibility, but has some advantages (for example: it's a bit faster).
Option #2 also need .htaccess
as you noticed, but in most cases RewriteRule
takes the following form:
RewriteRule (.\*) index.php
Where index.php
is your front controller.
The biggest advantage (IMO) of this soultion is that each route is described in PHP (or whatever you use) so accessing these routes, modifying them is much easier. Furthermore these routes can be used then not only for changing URL into set of variables, but also in opposite way - to create URL from set of variables.
I think the following example (from Symfony framework) will explain what I am talking about:
// apps/.../config/routing.yml - Describes routing rulespost: url: /read/:id/:slug params: { module: blog, action: index } requirements: { id: \d+, slug: \w+ }// apps/.../modules/blog/templates/indexSuccess.php - template for index action<?php echo link_to($post['title'], '@post?id=' . $post['id'] . '&slug=' . $post['slug']); ?>//creates: <a href="/read/123/my-first-blog-post.html">My first blog post</a>
Now whenever you change your rounting.yml
file and change /read/:id/:slug
into /:slug_:id
all your links in application will turn into /my-first-blog-post_123.html
.
Doing such and others things when you use option #2 is much easier.