Resume
It’s very common to do the URL rewriting while we are developing a site. Thus, many people wants to do this to make their site more beautiful. However, some of the tutorials are too complicated. This passage will tell you how to rewrite URL on Apache as easy as possible.
.htaccess
For instance, if we have blog.php
in the directory blog
. And we want to redirect a URL like http://example.com/blog/123
to http://example.com/blog/blog.php?id=123
. Then we need to do the URL rewriting.
/blog
./blog.php
First of all, create a file called .htaccess
in the directory that you want to rewrite.
Then, if you haven’t enabled the URL rewriting generally, write RewriteEngine On
in your .htaccess
file to enable URL rewriting.
Create rules
Then, in our .htaccess
file, just simply type RewriteRule ^blog/([0-9]+) blog.php?id=$1
.
Some readers may already know that the ([0-9]+)
is the regular expression which means that we are allowed to define what we want to accept in the URL.
Moreover, $1
means we take the first regexp as a variable.
TO BE CONTINUE…