How to change your site url structure

Most of us had this problem:

We found a link with an interesting title, click on it and all it showed was a “This page cannot be found message”

So why does this happen?

Well, the link most probably was valid some time ago, but site owners decided to change the site arhitecture. So, what was once http://www.example.com/article.php?id=243 is now http://www.example.com/article/243-some-cool-title. All seems ok, if you browse through the site,  but maybe hundreds of backlinks from other sites that are now broken, annoying users who thought they found something interesting.

The solution to fix this is actually very simple, even for big sites. All you have to do is to redirect the old urls, to the new urls, with a header status 301 (Page moved permanently).

There are many ways to do that, but the best solution is solely based on your site urls arhitecture (old and new).

I will show you an example of how the redirection can be achieved.

Let’s asume we have 2 links:

http://www.php-code.net/article.php?id=[article_id] 
http://www.php-code.net/category.php?id=[category_id]

that we want to change into

http://www.php-code.net/articles/[article_id]-[article_title] 
http://www.php-code.net/[category_id]-[category_title]

First, we need to make sure that you have mod_rewrite on your server (most web hosting providers have this setting by default).

Next, in  .htaccess file

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#You should put here pattern for all the links that needs to be rewritten:
RewriteRule ^(article\.php|category\.php) rewrite.php [L]
</IfModule>

And in rewrite.php

<?php
//Change $mainurl variable with your site url
$mainurl = '/';
//Correct way of doing redirection - add 301 Code to let browsers know that page is moved for good
header("HTTP/1.1 301 Moved Permanently");
//Dummy functions to make thhe script work
//You should replace this functions with specific code for your site
function get_article_url_title($id){
	return urlencode('Article-title');
}
function get_category_url_title($id){
	return urlencode('Category-title');
}
//...
if (preg_match('/article\.php/i',$_SERVER['REQUEST_URI']) && is_numeric($_GET['id'])) {
	$id = $_GET['id'];
	//get_article_url_title should be replace with a code that get the url encoded title for a given article id (e.g. an sql query on your articles table)
	$title = get_article_url_title($_GET['id']);
	header("Location:".$mainurl."articles/".$id."-".$title);
} elseif (preg_match('/category\.php/i',$_SERVER['REQUEST_URI']) && is_numeric($_GET['id'])) {
    //get_category_url_title should be replace with a code that get the url encoded title for a given article id (e.g. an sql query on your articles table)
	$id = $_GET['id'];
	$title = get_category_url_title($_GET['id']);
	header("Location:".$mainurl.$id."-".$title);
}
//...
?>

The code is pretty much self explanatory. All the links that have article.php or category.php inside, will be forwarded to rewrite.php, which in turn will construct the new url.

Ironically, changing the link structure is mostly done for SEO sake, but making this decision without proper handling of old links, can do more harm than good making you lose all the indexed pages relevance.
So, if you plan to restructure the site, the best thing to do is to try to use redirection for your old links.

Note:
This solution presented here might not work for you. It really depends on how your site is organized,  how your links are handled by the php scripts, if you use or used a dispatcher for all your links, like most frameworks do today, and many others. There is no generic solution, to work on all sites, and the code showed here is only to give you a head start.

No related posts.

3 Responses to “How to change your site url structure”

  1. Jarghsld says:

    I’d like to apply for this job 16yr girls erotiek wish that was me!!

  2. Brooklyn says:

    I’m on business nude model toplist lola haha lameass can’t even can’t his dick in

  3. Brady says:

    The National Gallery buspar available generic equality and justice, honesty e.g. QuEST/SCHOLAR-

Leave a Reply