5

When using URL rewriting for beautification, are there any thoughts on whether to do your content calculations in a PHP script or to hard code it into the .htaccess file?

For instance, WP adds the simple rule to the .htaccess file

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Which directs everything to the index.php page. Then it has a PHP script (canonical.php) parse the $_SERVER['REQUEST_URI'] to then figure out what content to actually pull up.

Instead of using PHP to handle all of this, it could be entered directly into the .htaccess file passing the query items from the URL, similar to:

RewriteRule ^products/([0-9][0-9])/$ /productinfo.php?prodID=$1 

Does anyone know the advantages/disadvantages of the two methods? I'm thinking that the PHP method offers a bit more flexibility, but I may be wrong. I have no idea of which has more overhead though.

Cœur
  • 32,421
  • 21
  • 173
  • 232
edeneye
  • 417
  • 4
  • 12
  • In my opinion, routing requests through a single PHP script allows you the opportunity to intercept invalid requests and handle the error appropriately. For example, a custom error page saying the page wasn't found, but here are some similar pages. – Jeremy Harris Feb 01 '12 at 15:14
  • That's what I was thinking. Seems much easier to use the PHP script to do all the handling on the fly. – edeneye Feb 02 '12 at 15:15

2 Answers2

2

My opinion is that PHP handling is much better. The only possible minus is that it could be slower, but not so much, in overall page generation time you won't notice any difference. But the disadvantages are:

  1. if your site grows, you have to provide new rules in .htaccess every time, which is not convenient. If you,for example, want to add one more GET argument to some script call, you have to modify the rules again.
  2. it is not comfortable, when you move from one host to another. in some cases you will need to change the directory structure, and all rules, which depend on it.
  3. if you, for some reason, decide to move your site from Apache to nginx, IIS, or some other server, you will have lots of trouble converting your .htaccess rules to some other server format.

So it's definitely better to handle these by PHP.

0

The 2nd method is surely not a good one. Since it requires you to add a new rule to .htaccess everytime that you want new section on your site.

For example you might want to add a new category you have to add

RewriteRule ^categories/([\w-]+)/?$ categories.php

For the performance/overhead, I think you don't have to micro optimize thing like this. Most of the time when it come to performance issues it always related with DB or I/O problems (Unless there's really bad code in app that slow the site down).

Rezigned
  • 4,523
  • 1
  • 17
  • 17
  • That's what I was thinking, and why the PHP method would be "better" from an ease of use standpoint. If there was a significant overhead difference, I'd have to consider one or the other. – edeneye Feb 02 '12 at 15:14