Skip to content

How to get entry from MYSQL depending on the entered url?

What I want to achieve is something like Wikipedia is using: You enter “” into your browsers search bar and wikipedia shows you the article for “Stack_Overflow”. I know I could do something using php’s GET and but I’d like to know how wikipedia’s solution works.


You need to rewrite ALL to index.php or router.php.

So in your .htaccess you can try the following:

# Rewrite ALL to index.php
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Then you can handle the request in your index.php file.

You can try somthing like that:

$reqguestedURL = trim($REQUEST_URI, '/'); // remove the leading and trailing '/'
$reqguestedURL = filter_var($reqguestedURL, FILTER_SANITIZE_URL); // sanitize the URL
$URL_array = explode('?', $reqguestedURL); // explode the URL
$destination = $URL_array[0]; // remove the query string
$queryString = $URL_array[1] ?? ""; // get the query string
$destinationParts = explode('/', $destination); // finally get the destination parts.

// so if the URL is:
var_dump($REQUEST_URI); //OUTPUT: /foo/bar?name=value
var_dump($reqguestedURL); //OUTPUT: foo/bar?name=value
var_dump($URL_array); //OUTPUT: Array ( [0] => foo/bar [1] => name=value )
var_dump($destination); //OUTPUT: foo/bar
var_dump($queryString); //OUTPUT: name=value
var_dump($destinationParts); //OUTPUT: Array ( [0] => foo [1] => bar )

And now you can get the value from your database.