Hi all!
I have model Test which have user defined url field – $Test->url (for example user set it to user-defined-clean-url )
I want change url for models’s pass action from
index.php?r=test/pass&id=3
to
user-defined-clean-url
How should I define the rule in urlManager for this case?
SOLVED
In actionPass instead of using $id param I use $url, and then by $url get id:
public function actionPass($url)
{
$id = Test::model()->find('url=:url', array(':url'=>$url))->id;
...
In model’s Test.php:
public function getUrl()
{
return Yii::app()->createUrl('test/pass', array(
/*'id'=>$this->id,*/
'url'=>$this->url,
));
}
In urlManager:
'<url:[a-zA-Z0-9-]+>'=>'test/pass',
One more problem…
Now work two types of link:
[list=1]
[*]
http://localhost/tests/index.php?r=test/pass&url=osobennosti-kharaktera
[*]
http://localhost/tests/user-defined-clean-url
[/list]
Is any possibility to disable first link to prevent duplicates in search engines?
Is it possible to define redirect from tests/index.php?any_params to tests/ or throw 404?
alirz23
(Alirz23)
August 14, 2012, 12:37pm
4
Is any possibility to disable first link to prevent duplicates in search engines?
Is it possible to define redirect from tests/index.php?any_params to tests/ or throw 404?
check this post out
http://www.yiiframework.com/doc/guide/1.1/en/topics.url
outrage
(Site Sense Web)
August 14, 2012, 12:48pm
5
As long as the unfriendly url isn’t exposed to search bots, either through the menu or links within the site, this shouldn’t cause issues with duplicate content.
If you are worried that the unfriendly link could be exposed, you can use 301 redirect to send it to the friendly url.
I know how HIDE index.php, but in this topic not described how DISABLE url with index.php
outrage:
As long as the unfriendly url isn’t exposed to search bots, either through the menu or links within the site, this shouldn’t cause issues with duplicate content.
If you are worried that the unfriendly link could be exposed, you can use 301 redirect to send it to the friendly url.
Google, Yandex toolbars can spy any urls, and then whey appear in search results.
Could you please explain that is the best way to create redirect in Yii?
outrage
(Site Sense Web)
August 14, 2012, 1:44pm
7
kernel32ddl:
I know how HIDE index.php, but in this topic not described how DISABLE url with index.php
Google, Yandex toolbars can spy any urls, and then whey appear in search results.
Could you please explain that is the best way to create redirect in Yii?
The best way would depend on how many urls may need redirecting.
For small numbers you can do it in .htaccess with a rule like such:
RewriteRule ^site/contact(.*)$ contact$1 [R=301,NC,L]
Here’s an example done in components/Controller.php:
http://angelotremonte.com/blog/2010/12/how-to-handle-301-redirects-for-a-moved-site-using-yii/
You could also do it in your controller action similar to the method above.
outrage , thank you for reply!
I decide to generate 404 if index.php is found in requestUri (components/Controller.php init()):
public function init() {
parent :: init();
if (strpos(Yii::app()->request->requestUri, 'index.php') !== false){
//this generate strange page with traceback
//throw new CHttpException(404,'The specified post cannot be found.');
//this generate empty page and 404 header
yii :: app()->request->redirect('', true, 404);
}