There is one more thing that we can do to further clean our URLs, i.e.,
hiding the entry script
index.php
in the URL. This requires us to
configure the Web server as well as the
urlManager application component.
We first need to configure the Web server so that a URL without the entry
script can still be handled by the entry script. For
Apache HTTP
server, this can be done by turning on the URL
rewriting engine and specifying some rewriting rules. We can create
the file
/wwwroot/blog/.htaccess
with the following content.
Note that the same content can also be put in the Apache configuration
file within the
Directory
element for
/wwwroot/blog
.
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
We then configure the
showScriptName
property of the
urlManager component to be
false
.
Now if we call
$this->createUrl('post/read',array('id'=>100))
, we would
obtain the URL
/post/100
. More importantly, this URL can be properly
recognized by our Web application.
7. Faking URL Suffix ¶
We may also add some suffix to our URLs. For example, we can have
/post/100.html
instead of
/post/100
. This makes it look more like a URL
to a static Web page. To do so, simply configure the
urlManager component by setting its
urlSuffix property to the suffix you like.
Thanks for guidance.
One more thing.
How can change static pages urls. E.g; following is url for About page.
http://localhost/main/yii/wf/site/page?view=about
How can I change it to more friendly url by removing '?' sign from url.
ANS:
Add the following line at first of components->urlManager->rules
'site/page/<view:\w+>'=>'site/page'
Thanks in advance
<?php
// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'My Web Application',
// preloading 'log' component
'preload'=>array('log'),
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
),
// application components
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
// uncomment the following to enable URLs in path-format
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false,
'rules'=>array(
'site/page/<view:\w+>'=>'site/page',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
'db'=>array(
'connectionString' => 'sqlite:protected/data/testdrive.db',
),
// uncomment the following to use a MySQL database
/*
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=testdrive',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
*/
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
// uncomment the following to show log messages on web pages
/*
array(
'class'=>'CWebLogRoute',
),
*/
),
),
),
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
// this is used in contact page
'adminEmail'=>'webmaster@example.com',
),
);