PHP dynamic DB page rewrite URL

How can I make —>

I want my DB query page to get it’s own URL, like I’ve seen on twitter etc etc.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

This is known as a “slug” wordpress made this term popular. Anyway though.

Ultimately what you need to do is have an .htaccess file that catches all your incoming traffic then reforms it at the server level to work with your PHP in the sense, you will still keep the ?id=123 logic intact, but to the client side ‘/folder/FHJKD/’ will be the viewable result.

here is an example of an .htaccess file I use a similar logic on.. (so does wordpress for that matter).

RewriteEngine On
#strips the www out of the domain if there
RewriteCond %{HTTP_HOST} ^www\.domain\.com$

#applies logic that changes the domain from
#to resemble
RewriteRule ^(.*)$$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?id=$1 [QSA,L]

what this will do is take everything after and pass it as a variable to index.php the variable in this example would be ‘id’ from this you have to device a logic that best suits your sites needs.


 //the URL for the example here:
   $myParams = explode('/', $_GET['id']);
   echo '<pre>';
   echo '</pre>';

now the logic for this would have to go much deeper, this is only pure example at a basic level, but overall and especially cause your working with a database I assume, your gonna wanna make sure the $myParams is clean of malicious code, that can inject into your PHP or Database.

The output of the above $myParams via print_r() would be:

   [0] => post
   [1] => my-article

To work with it you would need to do at the very least

echo $myParams[0].'<br />';

or you could do it like this cause most browsers will add a final /

 //the URL for the example here:
   //breaks the variable apart, removes any empty array values and reorders the index
   $myParams = array_values(array_filter(explode('/', $_GET['id'])));
   if(count($myParams > 1)
       $sql = "SELECT * FROM post_table WHERE slug = '".mysql_real_escape_string($myParams[1])."'";
       $result = mysql_query($sql);


Now this admitedly is a very crude example, you would want to work some logic in there to prevent mysql injection, and then you will apply the query like you would how you are now in pulling your articles out using just id=123.

Alternatively you could also go a completely different route, and explore the wonders of MVC (Model View Control). Something like CodeIgniter is a nice easy MVC framework to get started on. But thats up to you.

Solution 2

This can be achieved with mod_rewrite e.g. via the .htaccess file.

Solution 3

In your .htacess, you need add RewriteEngine on.

After that, you will need to do some regexs to make this little beast work. I’m assuming ?id is folder.php?id=123.

For example the folder piece: RewriteRule ^folder/([a-zA-Z0-9_-]+)/([0-9]+).html$ folder.php?id=$123

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply