301 redirect in cakephp (moved permanently in cakephp)

301 redirect in cakephp (moved permanently in cakephp)

How to permanently 301 redirect in cakephp?
Best way for 301 redirect (permanently redirect).
301 redirect without .htaccess file.

Recently, i am working with cakephp project, and my client need to redirect all the old links to new links. As its a ecommerce website, it have the category and products but the old php developer made single page with two parameter in the query string, where one is cid which indicate the category and another one is pid which indicate the product. all the links are not seo friendly and its look like as below

N Numbers of links in the project but i show 3 example where we need to do 301 redirect from old links to new links.

 
 // Old Links
 
  productcategory.php?cid=1&pid=3      // Category and Product Page 
  productcategory.php?cid=5&pid=31     // Category and Product Page
  productcategory.php?cid=22           // Only Category Page
 
 // New Links 
 
  product/1/electronics/3/iphone      // Category and Product Page 
  product/5/games/31/play-station-4    // Category and Product Page
  category/22/beauty/                  // Only Category Page

301 redirecting is not easy and we need to workout with the rewrite rules using .htaccess file but i did it with easily in CakePHP. In cakephp 301 redirect like this is possible without .htaccess file. you need to create the productcategory.php in your webroot folder and place below code in that.

 
 <?php
 
	$siteurl = "http://www.freelancephpdevelopment.com/";
	$cid = isset($_GET['cid']) ? $_GET['cid'] : '';
	$pid = isset($_GET['pid']) ? $_GET['pid'] : '';		
 
	switch($cid)
	{
		case '1':
				$CategoryName = "electronics";
				break;
		case '5':
				$CategoryName = "games";
				break;
		case '22':
				$CategoryName = "beauty";
				break;
	}	
 
	switch($pid)
	{
		case '3':
				$ProductName = "iphone";
				break;
		case '31':
				$ProductName = "play-station-4";
				break;		
	}
 
	if($cid != '' && $pid != '')
	{
		header("HTTP/1.1 301 Moved Permanently"); 
		header("location: ".$siteurl."product/".$cid."/".$CategoryName."/".$pid."/".$ProductName);
		exit;
	}
	else if($cid != ''){
		header("HTTP/1.1 301 Moved Permanently"); 
		header("location: ".$siteurl."category/".$cid."/".$CategoryName);
		exit;
	}
	 else {
		header("HTTP/1.1 301 Moved Permanently"); 
		header("location: ".$siteurl);
		exit;
	}

After reading my article, I hope that you can resolve your 301 redirect cakephp issue. still you are facing any issue then please let me know and i will be happy to help you.