Web Site User Friendly URLs with mod_rewrite, won’t work

Update: Please see the updated post, mod_rewrite – User Friendly URLs That Works.

I’ve read through many articles discussing “User Friendly URLs” and I have come to the conclusion that Apache module mod_rewrite and the use of an external prg (program) using preferably PHP but if necessary Perl, is the answer. If there is a better answer please let me know. As for now I can’t seem to figure out how to get my “User Friendly URLs” method to work using Apache mod_rewrite.

What I want to achieve is to create virtual paths such as www.domain.com/products/ or www.domain.com/products/erv/ with trailing slashes that is passed into mod_rewrite to process this virtual path. Which breaks up the path into separate values (i.e. “products”, “erv”). Then queries the database for the topic id to determine if “erv” and/or “products” exists. If it does exist the topic id would be returned to the page to be processed other wise a 404 error should occur to the client. I don’t want to have to input direct references to all the paths into Apache’s httpd.conf or .htaccess as I have 11 topics with 70+ sub-topics to deal with. Going to a database is the best option I believe. In theory this should work however I’ve tried my own mod_rewrite RewriteRules and RewriteMap with no success of a value being returned. I’ve even tried examples that demonstration a similar method but no success. If anyone has an idea how to achieve this or can direct me into the right direction please let me know.

PHP code example to break up the path into separate values.
list($value1, $value2, $value3) = explode(“/”, $_SERVER['SCRIPT_URL']);

httpd.conf mod_rewrite in VirtualHost directive.

RewriteMap content prg:/var/www/htdocs/venmar/getTopicId.php
RewriteRule ^(.*)/(.*)/$ /index.php?nTopicID=${content:$1|0}

Example of a Basic MySQL Query using PHP

A basic MySQL query using PHP with commenting marked with //.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// SQL statement that will be sent to the MySQL database server.
$szQry = "SELECT column1, column2 FROM foo";
 
// MySQL database login credentilas; host (127.0.0.1), username and password.
$szDBConn = mysql_connect("host","username","password");
 
// Select database using the login credentials provided above.
mysql_select_db("database_name", $szDBConn);
 
// Send SQL statement to database.
$saResults = mysql_query($szQry, $szDBConn);
 
// Fetch results retured back from the SQL statement.
$obResults = mysql_fetch_row($saResults);
 
// Close database connection
mysql_close($szDBConn);

MySQL Lost connection to MySQL server during query

For more than 3 hours I’ve been trying to find a solution to the error I kept receiving when doing a basic query to MySQL through PHP. It kept returning back to me “Lost connection to MySQL server during query”. Right away I went to my PHP, MySQL and Apache logs to find more information. I came back with nothing more than the error message I already received. So as I usually do off I went to Google and IRC for some help. Neither were leading into any direction to a solution. Even after finding the MySQL documentation talking about the “Lost Connection” or “server has gone away” error I didn’t find a solution. I started talking to dhartmei on EFNet in the #OpenBSD channel for some assistance. He stepped me through doing the following at the command prompt.

# nc -v 127.0.0.1 3306
Connection to 127.0.0.1 3306 port [tcp/mysql] succeeded!
Host ‘localhost.domain.com’ is not allowed to connect to this MySQL

# netstat -an | grep 3306
tcp 0 0 *.3306 *.* LISTEN

# mysql -h 127.0.0.1 -u username -p database
Enter password:
ERROR 2013: Lost connection to MySQL server during query

This determined that port 3306 was listening for connections however localhost/127.0.0.1 returned an error message of “Host ‘localhost’ is not allowed to connect to this MySQL”. If you haven’t caught on already this means I forgot to set MySQL permissions correctly for the user. Remember that localhost and 127.0.0.1 is different according to MySQL permissions.

A quick GRANT (i.e. grant all ON database.table TO username@localhost identified by ‘password-here’) command in MySQL resolved the problem.