Maybe, but you need to use RewriteMap to determine the mapping you can use in the RewriteRule
.
Apache version 2.2 does not have direct access to the database, so you need to write a script that executes the actual request and then returns the result. You can identify this card using the "External Rewriting Program" .
So, if you have a script that takes "cats" from stdin, then queries the database and returns "1", you would define it like this:
RewriteMap item_lookup prg:/path/to/item_lookup.php
This directive must be in your server or vhost configuration; it cannot be in the htaccess file. But you can use the mapping in the htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /item.php?id=${item_lookup:$1} [L]
So, this takes the URI /cats
and rewrites it to /item.php?id=1
.
If you are using apache 2.4, you can use the "DBD" card. You can insert the query directly into the map definition, bypassing the need to use an external script. You will use it in the same way.
RewriteMap item_lookup "fastdbd:SELECT id FROM items WHERE name = %s"
Then use it the same way:
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /item.php?id=${item_lookup:$1} [L]
Without using the DBD / FastDBD query, I think that you are just better off doing a database search with item.php
, since you duplicate all this work anyway in the second external script. Just add something like:
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^item/([0-9]+)$ /item.php?id=$1 [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([A-Za-z0-9-]+)$ /item.php?name=$1 [L]
And in your item.php
script check both id and name . If you have a name , search the database to turn it into an identifier. This is much easier to manage, you do not need to have access to the / vhost server configuration, and you do not complicate matters using a rewrite card.