Tuesday, September 15, 2009

1)Rewriting product.php?id=12 to product-12.html

It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.

RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1

2) Rewriting product.php?id=12 to product/ipod-nano/12.html

SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.

RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2

3) Redirecting non www URL to www URL

If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]

4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz

Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1

5) Redirecting the domain to a new subfolder of inside public_html.

Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1

Thursday, September 10, 2009

CakePHP URL-based language switching (internationalization and localization)

Write this below code in (app/config/routes.php) file.

Router::connect('/:language/:controller/:action/*',
array(),
array('language' => '[a-z]{3}'));

Wednesday, September 9, 2009

Internationalizing Your Cakephp Application(support multiple languages cakephp)

For some developers, allowing a website to support multiple languages is essential. Luckily cakePHP 1.2 has the foundations available to make this possible.

I certainly hope after using this guide you can quickly and easily implement multiple languages in your cake app without needing to skip around the place. If I fall short of this and you have suggestions, leave a comment.

Once you complete this tutorial your site will be able to:
1. display multiple languages
2. allow users to switch languages
3. store language settings in cookies, so returning visitors don't need to re-select their preferred language
The sites that I build typically require 3 languages:
1. British English(eng)
2. Koren (kor)
3. Japanes (jap)
Step 1: Setup the directories and file for your messages
app/locale/eng/LC_MESSAGES/ default.po
app/locale/kor/LC_MESSAGES/ default.po
app/locale/jap/LC_MESSAGES/ default.po

Example Code of app/locale/eng/LC_MESSAGES/ default.op file.
msgid "home"
msgstr "Home"
msgid "about_us"
msgstr "About us"
msgid "membership_options"
msgstr "Membership Options"
msgid "our_lessons"
msgstr "Our lessons"
msgid "community"
msgstr "Community"
msgid "contact_us"
msgstr "Contact Us"
msgid "faqs"
msgstr "FAQ's"

Step 2: Write some strings to translate.(.ctp view file code sample)


Step 3: Change the default language
// config/bootstrap.php
define(DEFAULT_LANGUAGE, 'eng');
Step 4: Let users change the language
Component Class:
class P28nComponent extends Object {
var $components = array('Session', 'Cookie');

function startup() {
if (!$this->Session->check('Config.language')) {
$this->change(($this->Cookie->read('lang') ? $this->Cookie->read('lang') : DEFAULT_LANGUAGE));
}
}

function change($lang = null) {
if (!empty($lang)) {
$this->Session->write('Config.language', $lang);
$this->Cookie->write('lang', $lang, null, '+350 day');
}
}
}
?>
Controller Class:
class P28nController extends AppController {
var $name = 'P28n';
var $uses = null;
var $components = array('P28n');

function change($lang = null) {
$this->P28n->change($lang);

$this->redirect($this->referer(null, true));
}

function shuntRequest() {
$this->P28n->change($this->params['lang']);

$args = func_get_args();
$this->redirect("/" . implode("/", $args));
}
}
?>
Controller Class:
//app_controller.php
class AppController extends Controller {
var $components = array('P28n');
}
?>
The final piece of code, are some custom routes that need to be added to cake/app/config/routes.php

//route to switch locale
Router::connect('/lang/*', array('controller' => 'p28n', 'action' => 'change'));

//forgiving routes that allow users to change the lang of any page
Router::connect('/eng/*', array('controller' => "p28n",'action' => "shuntRequest",'lang' => 'eng'));
Router::connect('/jap/*', array('controller' => "p28n",'action' => "shuntRequest",'lang' => 'jap'));
Router::connect('/kor/*', array('controller' => "p28n",'action' => "shuntRequest",'lang' => 'kor'));
Step 6: Links to change language
image("lang-3.gif", array("alt" => "english",'url' =>_ROOT.'lang/kor')); ?>

image("lang-2.gif", array("alt" => "english",'url' =>_ROOT.'lang/jpn')); ?>

image("lang-1.gif", array("alt" => "english",'url' =>_ROOT.'lang/eng')); ?>