Search code examples
.htaccesswebwebserver

Get friendly URL and query database


I need to know what web server and server side framework will let me achieve the following:

I don't know is there is a technical name for this technique. So basically want to get any url in my domain and use it to query a database.

So for example, if I navigate to http://example.com/jack.htm, I want to do "select * from users where name like %jack%"

and then I want to return in the web page something like:

Our user's named Jack:

  • Jack Martins age 26
  • Jack Johnson age 32 etc..

Obviously we have a sql database with all user's names and age. Also jack.htm does not exist as a htm document in my web server, I need to generate it on the fly but not actually keep it forever.


Solution

  • Your webserver needs to rewrite the URL and turn the part before ".htm" into a parameter for an existing page that uses this parameter to query the database.

    Eg. apache config

    RewriteRule /\(.*\)\.htm$  /querydb.php?$1 
    

    I'v no experience with .htaccess, but something like:

    RewriteRule ^\(.*\)\.htm$  /querydb.php?$1
    

    Watch out for SQL injection inside querydb.php

    jack.htm will never need to exist, and if it does, it won't be used.