I have a question about yii URL.
For example, http://localhost/example/index.php?r=site/login, how could I change it to http://localhost/example/login?
I am able to change it to http://localhost/example/index.php/login by editing protected/config/main.php.
Below is my urlManager setting.
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false, //<-added as suggested by Martin
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'login' => 'site/login',
),
),
My question is how can I remove(hiding) the 'index.php' in http://localhost/example/index.php/login ?
SOLVED
I move .htaccess file from inside protected to root directory, where index.php is.
www
- protected
- index.php
- .htaccess
Can refer to http://www.yiiframework.com/wiki/214/url-hide-index-php/
Add “'showScriptName' => false,” to the rules array.
Related
I have a controller called "SitemapController". This easily maps to this url:
/sitemap
However, I need that link to also work if you open
/sitemap.xml
How do I map it in YII so the system can use both?
you can achieve it like below by editing config file ,
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'sitemap/*'=>'sitemap/index',
'sitemap.xml/*'=>'sitemap/index',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
Now it will map routes to sitemap/someKey/someValue/..
Add this to your url routing 'sitemap' => array('site/sitemap', 'urlSuffix'=>'.xml'),
You can modify your url manager in config/main.php this way,
'site/sitemap.xml'=>'site/sitemap',
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'site/sitemap.xml'=>'site/sitemap',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
In your .htaccess add this rule
RewriteEngine On
RewriteRule ^sitemap.xml.*$ http://example.com/sitemap/ [R=301,L]
I have Yii project and change my language in config/main.php to fa_ir, and set components basePath = protected/messages but don't changed basePath, yet is selected fa_ir from framework directory !!!
my project main.php code :
return.array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'New web site',
'defaultController'=>'site',
//'timezone'=>'Asia/Tehran',
'language'=>'fa_ir',
// preloading 'log' component
'preload'=>array('log'),
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
'application.extensions.*',
),
'modules'=>array(
// uncomment the following to enable the Gii tool
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'123',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters'=>array('127.0.0.1','::1'),
),
),
// application components
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
'messages'=>array(
'basePath'=>Yiibase::getPathOfAlias('application.messages'),
),
// uncomment the following to enable URLs in path-format
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
and ...
So directory files is : protect/message/fa_ir/yii.php and zii.php
I am very grateful from respondents.
Try it with a single language:
'language'=>'fa',
Or:
'language'=>'ir',
And use the same for your directory.
I have controller and module with same name: download
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'http://'.SITE_DOMAIN.'/<action:(download)>/<url:.*>'=>'<action>',
'http://<module:(download)>.'.SITE_DOMAIN.'/<code:\w{32}>'=>'<module>',
),
)
So I want to links like: http://domain.com/download/dir1/dir2/file1.zip
to be routed to: application/controllers/DownloadController
where $_GET['url']=='dir1/dir2/file1.zip'
And links like: http://download.site.com/some_code
to be routed to: application/modules/download/controllers/DefaultController.php
where $_GET['code']=='some_code'
But now both types of links are routed to: application/modules/download/controllers/DefaultController.php
I can't understand why
Try with this:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'http://'.SITE_DOMAIN.'/download/<url:.*>'=>'Download/index',
'http://download.'.SITE_DOMAIN.'/<code:\w{32}>'=>'Download/Default/index',
),
)
Note: both URLs will be routed to the index action of they own controller.
I am trying to use the following url: (With the htaccess applied)
/user/{username}
to redirect to something like:
/user/view/{username}
I have no clue what the rule should be...
My current urlManager config is:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
)
),
I'm also going to want some overrides such as (That go to the regular action)
/user/settings
/user/edit
etc... how would I add these?
Figured it out on my own!
'user/settings' => 'user/settings',
'user/<action>/*' => 'user/view/user/<action>',
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<view>' => array('site/page/view/'),
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
//'index' => array('site/index'),
),
),
I currently have that in my main.php file.
The problem I have is that when I view /index.php/index I have shown the index page in the pages folder but when I get to /index.php/about I still get the index.php file in the pages folder.
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
//'index' => array('site/index'),
'<view>' => array('site/page/view/'),
),
),
It should have been like this:
Now the rule should be (at least in version 1.1.12)
'<view:\w+>' => 'site/page',
This code will provide $_GET['view'] to SiteController::actionPage, e.g. http://example.com/test will set $_GET['view'] = 'test'