Mod-rewrite with PHP where vanity URLs are stored in MySQL

I have a page called “company.php” in the root of my server. This file is used to display information on a specific company using an id, for example “company.php?id=85”

I am trying to use mod-rewrite to rewrite “company.php?id=85” to have a vanity URL like “microsoft.php”. The vanity filename “microsoft.php” i stored in MySQL in the “company” table and the “mod_url” column.

I have read several tutorials, but I cannot make it work.

Could someone please give me a solution for this specific problem? I need to know EXACTLY what to write in the “.htaccess” and “company.php” files.


Unless you have access to the vhost.conf or httpd.conf files, you will have to make do with a PHP request handler (you cannot use a RewriteMap to do a lookup of your company name to fetch its id.

To create a PHP file handler is simple:

  1. Do NOT output anything from the PHP script.
  2. Read the company name requested.
  3. Convert all company name input to lowercase (and be sure that matches the database).
  4. Fetch the id from the database for a match
  5. If no match, set the id to that for your DEFAULT page (garbage in => garbage out - provide a sitemap for your default page).
  6. Use a meta tag to set the status to 200 if the id is found else 404.
  7. Use a location:URI redirection with zero delay to the company page OR to the default page.

In the .htaccess,

  1. Test whether the {REQUEST_FILENAME} is NEITHER a file or directory
  2. Redirect everything before the (IMHO, unnecessary) .php to your handler script with the company name.

Of course, in your handler script, you will need to protect your website against hackers trying to break your database/website.

IMHO, your “EXACTLY” is too close to a “script kiddie request” so you’ll have to make do with the pseudo code directions above. If you need help with the mod_rewrite code, have a look at the coding examples at If you have questions after that, please show your code when asking.



This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.