Clean URLs with mod_rewrite and .htaccess

Do you stop and think that the .php  file extension on your files looks ugly and unnecessary?  I do. When I’m creating a project, one of the first things I do is get rid of those file extensions.

Here’s a quick and easy way to do that using <a href=”http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html”>mod_rewrite</a> in your .htaccess file. (<i>Note: You will need to have the mod_rewrite module enabled, and the directory will need <a href=”http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride”>AuthOverride</a> All on, in the apache configuration</i>)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php

This code basically tells mod_rewrite that if the requested filename is not a file, and not a directory (the first 2 lines), then rewirte the URL as %{REQUEST_FILENAME}.php.  Take this code, create a new file in your web root, named .htaccess, paste the code in and save it. That’s all there is to it.

So if you have a file called “file.php” on your domain (http://example.com/), you should now be able to access it by going to http://example.com/file rather than http://example.com/file.php.

Posted in .htaccess, Apache, URLs, mod_rewrite at March 10th, 2009. No Comments.

MySQL Tip: Use unsigned int for larger values

Have you ever created an <b>int</b> field for a primary key? Or perhaps to store a numeric value? Chances are, you probably have, and if you don’t know what <b>unsigned</b> means, you are limiting the number of possible values that can be used in that field.

In MySQL, a field with the data type of int, without unsigned specified is capable of storing negative values. Since auto incremented primary keys will not use negative numbers for identifiers, you are limiting yourself to effectively half of the possible values of the int data type (between 0 and 2147483647).

That may seem like a fairly large number to most of you, but why take the risk?  If you specify your integer field as <b>unsigned</b>, you will double the number of values accepted by the field.

<b>Why?</b>  A <b>signed</b> integer, which in MySQL are all integers that are not specified as unsigned, will use the first of 32 bits as a marker for negative or positive.  This means that the possible values for are 2^31.  With an unsigned integer, the values are 2^32, or 0 to 4294967295.

The same exact rules apply for BIGINT (which is signed 2^63 and unsigned 2^64).

Posted in Datatypes, MySQL at March 7th, 2009. No Comments.