PHP retrieving data from MYSQL and displaying as new page

I have a website where users submit data to a database. When retrieving the data in the database i want it to display it as a new page. For example, if a user submits a post titled “this is a new post” i want php to take that from the database and display it in the url as www.something.com/this-is-a-new-post instead of www.something.com/index.php?title=this%is%a%new%post

I see multiple sites do this and i have no clue how they do it. I’m not sure if php actually creates a unique directory and index.php for each post or if it somehow just covers up the sloppy url with a fake one for search engine and display purposes…

It’s done with .htaccess and mod_rewrite. It can be a bit tricky to deal with if you don’t know about htaccess at all but it’s only a couple lines of code to achieve.

Use a .htaccess file to rewrite the URL:

[php]RewriteEngine On
RewriteRule ^title/([^/]*).html$ /index.php?title=$1 [L]
[/php]

The original URL:
http://www.something.com/index.php?title=this%is%a%new%post

The rewritten URL:
http://www.something.com/title/this%is%a%new%post.html

Obviously, when you create your URLs, don’t use a space use another separator such as - or _ which would make it like:

http://www.something.com/title/this-is-a-new-post.html

Of course you could change /title/ or remove it completely!

Try this generator, play about and see the results for yourself: http://www.generateit.net/mod-rewrite/

Sponsor our Newsletter | Privacy Policy | Terms of Service