Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to handle friendly/clean/pretty URLs with mod_rewrite or a language like PHP?

I'm developing my first decent-sized PHP site, and I'm a bit confused about what the "right way" (assuming there ever is such a thing) to handle clean/friendly/pretty URLs in the application.

The way I see it, there are two main options (I'll use a simplified social news site as an example):

1. Use mod_rewrite to handle all potential URLs. This would look similar, but not identical, to the following:

RewriteRule ^article/?([^/]*)/?([^/]*)/?([^/]*) /content/articles.php?articleid=$1&slug=$2
RewriteRule ^users/?([^/]*)/?([^/]*) /content/users.php?userid=$1&username=$2
RewriteRule ^search/?([^/]*)/? /content/search.php?query=$1

2. Pass everything to some handler script and let it worry about the details:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) handler.php?content=$1

Clearly this is all untested "air code," but you get the point.

  • Is one of these two ways going to be seriously slower than the other? Presumably the mod_rewrite is slower, since I'll be forced to use .htaccess files for this.
  • Are there serious disadvantages to either of these approaches?
  • Is there a "best practice" for this sort of thing, or is it something that each developer tends to decide for themselves? I know WordPress uses option two (though it was more trouble than it was worth when I investigated exactly how they did it).
like image 252
AgentConundrum Avatar asked Jan 23 '10 10:01

AgentConundrum


2 Answers

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.


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.

like image 95
Pascal MARTIN Avatar answered Oct 25 '22 13:10

Pascal MARTIN


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.yml
homepage:
  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') ?>
like image 25
Ben James Avatar answered Oct 25 '22 14:10

Ben James