My digital scrapbook

Redirecting invalid URI

on under Uncategorized • Be the first to comment

I was run my website using BlogEngine.Net and now using wordpress as mention before I was trying importing my old blog entries using BlogML. My old website using URL rewriter like blogengine supported and all ended with .aspx. This will add error to Google Webmaster tools as 404 a.k.a Not Found.

There’s so many link back to old URI and I think to redirect automatically. There’s a status code that Google bot understand that the old URI has been invalidated, the famous 301 Permanent Redirect. There’s so many plugin offer redirection and most of all doesn’t fit my needs.

So, I began to create rambling code using Notepad++ 🙂


echo redirect(basename('/post/2007/12/Shane-and-Peter%27s-Interview.aspx'));

function redirect($uri)
	$pattern = "/(.*)\.aspx/i";

	preg_match($pattern, $uri, $matches);
	echo $new_slug = urldecode($matches[1]);
	echo $new_slug = str_replace('\'', '', $new_slug);

The code is self explanatory. I want to redirect old one’s to the new one, but we need to extract permalink data to be used on wordpress post search. I am quite lazy today and I still looking some plugin which just redirect from the old uri to the new one.

The problem I meet was there’s still non alphanumeric characters on old URI, so I add required replacement.

Luckily, I found one from and I don’t need to reinvent the wheel for creating plugin. Hacking a function little bit then viola

function bas_permalink_moved_permanently() {
	 if( is_404() ) {

	 	$pattern = "/(.*)\.aspx/i";

		$slug = basename( $_SERVER['REQUEST_URI'] );	

		if( preg_match($pattern, $slug, $matches) ) {
			$single_slug = urldecode($matches[1]);
			$single_slug = str_replace("'", '', $single_slug);

			if( $ID = bas_post_exists_elsewhere( $single_slug ) ) {
				bas_forward_to_new_location( get_permalink( $ID ) );

That’s dirty code and I just need that, so there’s no advanced programming required and Google still find my old post 🙂