If you work with PHP, you notice that this URL: http://yoursite.com/index.php?post=2
is not pretty at all. There is term “Pretty URL” and a lot of tutorials how to make your URL prettier. In this post I’m going to share a few line of .htaccess
codes to make your URL more prettier.
Let’s go back to that URL. Here is the goal, we want to make a fake URL like this: http://yoursite.com/post/2/
that redirects us behind the scene to that ugly URL (http://yoursite.com/index.php?post=2
).
Here is what we need to add to our .htaccess
file:
RewriteEngine On RewriteRule ^post/(.*)$ index.php?post=$1
Read this post to know how to create an .htaccess
file: http://zofiakreasi.com/htaccess-tutorial-removing-php-extension/
$1
in that code is a variable. So if a user opens this URL: http://yoursite.com/post/110/
then the value of $1
is number 110
.