How to create friendly URL in php?


Normally, the practice or very old way of displaying some profile page is like this:

where u=12345 is the user id.

In recent years, I found some website with very nice urls like:

How do I do this in PHP?

Just as a wild guess, is it something to do with the .htaccess file? Can you give me more tips or some sample code on how to write the .htaccess file?

10/7/2013 6:16:02 PM

According to this article, you want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:

RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1

And this maps requests from




Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:

<Files news>
    ForceType application/x-httpd-php

And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:

    echo $_SERVER['PATH_INFO'];
    // outputs '/63.html'
2/24/2016 5:25:00 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow