The URL structure of your site is very important for both users and search engines. A short URL, that gives some indication of what can be found on the page is more trustful, than a very long URL with a lot of parameters that can be difficult to understand.
From a SEO perspective, if important keywords are included in the url, this can increase the overall seo credibility of the page.
In this post you’ll learn to build 2 types of nice URL’s for dynamic sites: .htaccess url rewriting and URL parsing techniques.
If you don’t have access to create and modify a .htaccess file, or your server isn’t based on Apache, or the Apache module “mod_rewrite” isn’t installed, the URL parsing technique can be very efficient. The basic principle in this technique is, that parameters to the page are included in the URL, and a piece of code is located in the top of the page, that can parse the parameters in the URL into variables.
Let’s start with the dynamic php page with the following URL:
In this example the page has two variables: “month” and “topic”, that has the values: “november” and “programming”. To pass on these values in the URL in a shorter way, can be done in the following way:
This is shorter than the starting point, and even more important, there are still some search engines that don’t like to index URL’s containing parameters with “?”. So this URL format is nicer to look at, at it increases the chance that the page is indexed by search engines.
The URL itself doesn’t do the entire trick, we also need a price of code that can parse the url into variables to be used in the script. To parse the URL into variables, the following code should be inserted at the top of the php code at the page:
list($month, $topic) = explode(‘/’, substr($PATH_INFO,1));
What the codes does, is taking the entire URL, stripping away anything until after “.php” and then splits the remaining information into variables, divided by “/”. The first parameter is called “month” and the next is called “topic”. There you go now, now the parameters “month” and “topic” are available for the script.
If you have access or is allowed to use ‘htaccess files, URL rewriting is by far the nicest and easiest way to make SEO friendly URL’s. To make URL rewriting possible the module mod_rewrite needs to be installed in your Apache configuration.
First you need to open up/create your .htaccess file, and add the following line
This command activates the url rewriting features in Apache. Next we need to create a nice way to serve the required variables to the file: www.example.com/articles.php. To do this add the following line to your .htaccess file:
RewriteRule article/([^/]+)/([^/]+).html /article.php?month=$1&topic=$2
This line tells Apache, that if a server request has the following format “www.example.com/article/november/programming.html”, the following things should happen:
- This request should be sent to the file: article.php
- The first parameter can be found after the first slash. The name of this parameter is “month” and the value is “november”
- The second parameter can be found after the second slash. The name of the parameter is topic, and the value is “programming”.
There you go, you now have a very nice URL, that makes sense for the users and is SEO friendly.
To make this work outside this simple example, there is a few things to notice:
- The parameter combination needs to be unique. Otherwise only one version of the content will be displayed
- Page titles are great to include in the SEO friendly URL, however special characters, spaces etc., should be converted into a standard format: The article with the title: “How to make SEO friendly URL’s” needs to be transformed before it can be used. You could make a transformation rule that replaces a space with “_” and removes special characters, so the title will be transformed to: “How_to_make_SEO_friendly_URLs”.