Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For URL rewriting, use .htaccess or PHP to determine content used?

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.

like image 219
edeneye Avatar asked Nov 05 '22 06:11

edeneye


1 Answers

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.

like image 109
Alexander Gilmanov Avatar answered Nov 07 '22 21:11

Alexander Gilmanov