Is it possible to query the database using the value passed in the url and write the result of the query to the url using mod_rewirite?

Is it possible to use mod_rewrite to write an htaccess rule that takes the value of the url parameter (for example: id = 1, where "id" is the parameter and "1" is the parameter value), query the database with the parameter value specified, and then write the value returned from the request as part of the URL of the requested page?

I know the basics of mod_rewite, for example, by rewriting a URL that looks like this:

www.example.com/item.php?id=1 

to the next:

 www.example.com/item/1 

An example of what I need is a record of the following URL:

 www.example.com/item.php?id=1 

:

 www.example.com/item/name-of-item-based-on-id-specified-in-original-url 

However, I don't know if what I want to do is possible using mod_rewrite.

If anyone has a solution to this problem, I would be very grateful if you could help me. If what I'm trying to do is not possible using htaccess and mod_rewrite, can someone please tell me how I can solve this problem?

Thanks.

+4
source share
1 answer

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.

+4
source

Source: https://habr.com/ru/post/1500598/


All Articles