URL Rewrite Tutorial With .htaccess

The internet seems like a beautiful place. Doesn’t it? The URL  https://www.facebook.com/DwayneJohnson/ will take you to Dwayne Johnson’s Facebook page.
For our very own website, the URL https://www.theitstuff.com/category/internet/page/2 takes you to the 2nd page in the internet category of articles. As simple as it may seem, this doesn’t work out of the box. The original URL would be something like https://www.theitstuff.com/blog.php?category=internet&page=2
But such a URL would be really inconvenient to remember, hence we use the one mentioned before. So in this article let us find out how we can create pretty URLs using apache’s mod_rewrite or a2enmod as some like to call it.
Before we being, I am assuming you know what an apache server is and have worked on a website with server scripting language like PHP.
So we will be using the URL rewrite module on apache. It should be enabled by default. In case if it isn’t you can just remove the # in front of the line that reads
LoadModule rewrite_module modules/mod_rewrite.so
from the httpd.conf file located in  xampp/apache/conf
edit httpd.conf notepad++
For Linux systems, the following command will enable it –
sudo a2enmod rewrite
After this just restart your apache server.
Now we need to create a file called the .htaccess which will do our URL rewriting work. So just create a file with the name .htaccess and save in your root directory.
edit server .htaccess
You can see that I have a .htaccess file and some other PHP files as well. Now let us open up the .htaccess file and write a simple rewrite rule.
So let us say you have a profile page and currently the scenario is such that when someone visits the profile page they actually access the file called profile.php  and the URL bar shows the URL as www.websitename.com/profile.php.
To make it a little easier on the eyes it would be better if we could remove the .php extension. To do this we will write a rule like this.
RewriteEngine On
RewriteRule    ^profile/?$  profile.php [NC, L]
Let us break this down.

  1. RewriteEngine On is the first thing to write in your .htaccess file. This needs to be written just once per file and at the very beginning. This simply turns the URL rewrite Engine On.
  2. RewriteRule defines a new rule, anything after this is considered a URL rewrite rule
  3. ^profile/?$   profile.php You must already know this if you have some experience with regular expressions. The string ‘profile’ is matched and if it matches, the file profile.php is served.

Since I am on my localhost currently this looks similar to –
make url in .htaccess php
You can see above that when I visit the URL localhost/profile on my web browser, I am able to access the URL localhost/profile.php file. So this is how it works.

  1. [NC, L] These are actually flags that denote special meanings. NC means not case-sensitive It tells Apache to ignore lowercase and uppercase characters. So even if you type localhost/PRofiLE you will see the same result. If in case you do not include the NC flag you might get a URL not found error in some cases. L flag tells Apache that this is the last match to check. If this is matched, then no other RewriteRule needs to be checked.

So this was a simple example of a URL rewrite. Now let us say you wanted to pass arguments via the GET method. A simple get method URL to access somebody’s profile page would be
localhost/profile.php?username=john
When rewriting this URL, it would be really cool if we can convert it into
localhost/profile/john
This would be easier to remember and cleaner as well. To implement this we will need to pass the username argument to the profile page in form of a get variable.
RewriteRule    ^profile/([A-Za-z0-9-]+)/?$ profile.php?username=$1
The syntax is similar to the one we did previously, however you can notice that we have passed the username argument here.
([A-Za-z0-9-]+) means that any character from that charset will be accepted.
profile.php?username=$1 means that the profile.php page will be served and a GET variable called username will be passed which will be typed after profile/.
.htaccess url building
You can see that the get variable is passed to the profile page which uses it in displaying an output.
If you ever wanted to pass more variables, Let’s say you wanted a URL like
products/food/2
You could use the following Rewriterule
RewriteRule    ^profile/([A-Za-z-]+)/([0-9-]+)/?$    products.php?category=?$1&page=$2
You can see that we added a forward slash and later another charset condition. You can see on the right side of the rule that the URL uses $1 and $2 for the GET variables, These variables are set in precedence of their appearance in the rewritten URL. Similarly, you can use $3, $4 etc.

Conclusion

So we discussed URL rewrite and learned some techniques. This is just a basic tutorial to get you started. A more comprehensive guide can be found from apache’s own website.

Read more