Creating Seo clean urls in yii1.1.x - php

i am creating seo clean url for my site .
i want to display userdetails, if user hit the url like http://localhost/2015/mysite/user/username for that i have created urlManager in yii
ex:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
array(
'class'=>'ext.sitemapgenerator.SGUrlRule',
// Optional. Default: '/sitemap'
),
'<view:(about|guidelines|faq|termsofuse|privacypolicy|help-us|spread-word)>' => 'site/page',
'contact'=>'site/contact',
'/'=>'site/index',
'<controller:\w+>/<action:\w+>/<id:\d+>/<active>/*'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>/*'=>'<controller>/<action>',
'<action:(login)>' => 'site/login',
'<action:(signup)>' => 'user/signup',
'user/<username:\w+>/*' => 'user/userdetailsseo', //user is my controller
),
)
So Now if the user search username i am redirecting to user/userdetailsseom
'/<username:\w+>/*' => 'user/userdetailsseo' working perfect for http://localhost/2015/mysite/username,
'user/<username:\w+>/*' =>'user/userdetailsseo' it's not working for http://localhost/2015/mysite/user/username i am getting page not found error...
Anyone can help me please...
Thanks..

Related

Yii User-friendly url and keep the old get format working

I set to the main config the following url rules:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>'=>'<controller>/list',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<controller:\w+>/<id:\d+>/<title>'=>'<controller>/view',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'\?r=<controller:\w+>/<action:\w+>' => '<controller>/<action>'
),
),
Everything is woking fine, but I also want the previous url format to keep working so I don't have to rewrite lots of urls that I don't care it to be seo-friendly:
index.php?r=controller/action&param1=value1
But it shows an error now. Is there a way to keep it working?
To my opinion the best way is to replace all old urls with your IDE regex replace option. But anyway you can do what you want this way:
Use following route rule in the urlManager config:
'rules' => [
[
'class' => 'my\namespace\UrlRule',
'pattern' => '<controller>/<action>',
'route' => '<controller>/<action>',
],
...
Extend yii\web\UrlRule with your my\namespace\UrlRule and rewrite its 'parseRequest' method so that it could use $_GET['r'] parameter value if is set:
namespace my\namespace;
class UrlRule extends \yii\web\UrlRule
{
public function parseRequest($manager, $request)
{
if ($pathInfo = \Yii::$app->request->get('r')) {
\Yii::$app->request->setPathInfo($pathInfo);
}
return parent::parseRequest($manager, $request);
}
}
You may also extend yii\web\Request instead so that it's 'getPathInfo' method could use $_GET['r'] parameter if set.

YII friendly SEO url

I am struggling with this problem and not found any suitable answer.
I want my_site/user/1 to become my_site/user/user_name
My urlManager looks like this:
'urlManager' => array(
'urlFormat' => 'path',
'rules' => array(
'user/<username:\w+>'=>'user/view',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
'showScriptName' => false,
),
User Controller:
public function actionView($username)
{
$this->render('view', array(
'model' => $this->loadModel($username),
));
}
When i hit /user/admin it redirects me to actionView not to the actionAdmin from my controller and i have multiple such actions like create, delete and so on.
How can i avoid this? Is there an alternative?
Thanks.
The urlManager try's to match the url to a controller and action.
the 1st part of
'user/<username:\w+>'=>'user/view',
has matched to the user controller and the next part it looking for any string that consists of words, then sent the user to user/view
I recommend changing user/<username> to manage-user/<username> or my-account/<username>, something like that.

YII urlManager rewrite singe URL with multiple parameters

Ok, removed my previous question and reformulated the new one, otherwise no one would understand, sorry. So here the problem that's left:
So I want to rewrite an URL to a single word which works with zero, one and two parameters, but not with 3:
Original url with 2 paramaters:
index.php?r=site/page&view=exposities&tijd=nu
urlmanager rule:
'exposities_nu'=>array('site/page', 'defaultParams' => array('view' => 'exposities', 'tijd'=>'nu')),
result:
/exposities_nu/
Now what doesn't work:
Original url with 3 parameters:
index.php?r=site/page&view=exposities&tijd=vestiging&locatie=1
urlmanager rule:
'knsm'=>array('site/page', 'defaultParams' => array('view' => 'exposities', 'tijd'=>'nu', 'locatie'=>'1')),
result:
index.php?r=site/page&view=exposities&tijd=vestiging&locatie=1
Does anyone why this last url isn't shortened to /knsm/?
Maybe you didn't set the url in the URL Manager right. See this example:
return array(
......
'components'=>array(
......
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'post/<id:\d+>/<title:.*?>'=>'post/view',
'posts/<tag:.*?>'=>'post/index',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
),
);
Also you have to create you URLs correct:
Yii::app()->createUrl('post/view', array(
'id'=>$this->id,
'title'=>$this->title,
));

URL Manager - action parameter optional and link creation

I have below URL mapping in my main.php file . You can see I am expecting parameter id as an optional
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'urlSuffix' => '/',
'useStrictParsing' => true,
'rules'=>array(
'' => 'site/index',
'<action:(signIn|signUp|logout)>' => 'site/<action>',
'<controller:\w+>(/<id:\d+>)?'=>'<controller>/index',
'<controller:\w+>/<action:\w+>(/<id:\d+>)?'=>'<controller>/<action>',
),
),
with the above rules If I create a link using below code
<?php echo CHtml::link('×', array('index', 'id' => $this->group_id), array('class' => 'linkclose')); ?>
this creates link some thing like below , which is wrong
http://localhost/blog(/872280)?/
It should generate some thing like below
http://localhost/blog/872280/
If I don't pass the parameter in the link I mean
<?php echo CHtml::link('×', array('index'), array('class' => 'linkclose')); ?>
this generates
http://localhost/blog/index
which is fine .
But with the parameter passing it is messing up the link .. Could some one help me on this ? Thanks
URL rules in their entirety are not regular expressions. Thus, optional groups like (...)? aren't honored. You can work around this by specifying alternative rules:
'rules'=>array(
...
'<controller:\w+>/<id:\d+>'=>'<controller>/index',
'<controller:\w+>'=>'<controller>/index',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),

Allowing only clean URLs in Yii

I have configured the CUrlManager in the config/main.php to use clean URL:
'urlManager' => array(
'showScriptName' => FALSE,
'urlFormat' => 'path',
'rules' => require(dirname(__FILE__) . '/routes.php'),
),
The clean URL function works perfectly, but I would like to prevent the default <controller>/<action> pattern match to occur.
This is my config/route.php:
<?php
return array(
'books' => 'book/index'
);
Now people can go to the same book page by 2 different URLs:
http://www.mysite.com/books
http://www.mysite.com/book/index
I want to disable the second URL pattern. Is this possible?
You can enable useStrictParsing in your url manager component.

Categories