Register here!

Forgot password?

ASK NEW QUESTION

Home Page » Url Management » How to setup urlManager to make url more friendly for SEO
0 Votes
Vote Con!

How to setup urlManager to make url more friendly for SEO

seo friendlyurlManager

Hello Everyone!,

How to setup urlManager to make url more friendly as like as http://yiianswers.com or http://yiiframework.com or http://mydomain.com/post/up-coming-calendar/year-2012

Please help me asap.

Thanks in Advance

By Hrishita in Url Management · Asked 608 days 21 hours 42 mins ago

Questions: 1 Accepted: 0 ( 0% ) | Reputation: 0

 

Answers (6)

  1. 0 votes

    1. Open file config/main.php
    2. Find and update line “urlManager” according to your needs.

    As an example
    When you type
    http://www.youdomain.com/whatever

    you want to show what’s in your (controller => site) (action =>index)
    http://www.youdomain.com/site/create/index.php

    You will have to update code to

    1.         // uncomment the following to enable URLs in path-format
    2.         'urlManager'=>array(
    3.             'urlFormat'=>'path',
    4.             'showScriptName' => false,
    5.             'rules'=>array(
    6.                 'whatever' => array('site/create'),
    7.                 '/'=>'/view',
    8.                 '//'=>'/',
    9.                 '/'=>'/',
    10.                 //'~'site/page'
    11.             ),
    		// uncomment the following to enable URLs in path-format
    		'urlManager'=>array(
    			'urlFormat'=>'path',
    			'showScriptName' => false,
    			'rules'=>array(
    				'whatever' => array('site/create'),
    				'/'=>'/view',
    				'//'=>'/',
    				'/'=>'/',
    				//'~'site/page'
    			),

    Also you will have to update file .htaccess

    1. Options +FollowSymLinks
    2. IndexIgnore */*
    3. RewriteEngine on
    4.  
    5. # if a directory or a file exists, use it directly
    6. RewriteCond %{REQUEST_FILENAME} !-f
    7. RewriteCond %{REQUEST_FILENAME} !-d
    8.  
    9. # otherwise forward it to index.php
    10. RewriteRule . index.php
    Options +FollowSymLinks
    IndexIgnore */*
    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

    For more information please look

    here,
    here
    and here

    By tuolden · 608 days 20 hours 56 mins ago

    Questions: 25 Accepted: 0 ( 0% ) | Reputation: 0

    • Thanks tuolden, I did all that things, please take a look below: [code] 'urlManager' => array( 'urlFormat' => 'path', 'rules' => array( array('class' => 'ext.sitemapgenerator.SGUrlRule'), '/about' => '/site/page/view/about', '/sitemap' => '/site/sitemap', '/sitemap.xml' => '/site/sitemapxml', '/press-and-media' => '/site/pressandmedia', '/privacy-and-policy' => '/site/privacyandpolicy', // '/blog' => 'wordpress', '/' => '/view', '//' => '/', '/' => '/', ), 'showScriptName' => false, ), [/code] And .htaccess as you written. As you written above, are you sure will handle like below url http://yiianswers.com/url-management/how-to-setup-urlmanager-to-make-url-more-friendly-for-seo/ In above URL my config tell url-management is controller and how-to-setup-urlmanager-* tells this is action. But problem is function actionHow-to-setup-urlmanager(){} this is not valid. So, please help me again Thanks

      By Hrishita · 608 days 6 hours 59 mins ago

    • Thanks tuolden, I did all that things, please take a look below: 'urlManager' => array( 'urlFormat' => 'path', 'rules' => array( array('class' => 'ext.sitemapgenerator.SGUrlRule'), '/about' => '/site/page/view/about', '/sitemap' => '/site/sitemap', '/sitemap.xml' => '/site/sitemapxml', '/press-and-media' => '/site/pressandmedia', '/privacy-and-policy' => '/site/privacyandpolicy', // '/blog' => 'wordpress', '/' => '/view', '//' => '/', '/' => '/', ), 'showScriptName' => false, ), And .htaccess as you written. As you written above, are you sure will handle like below url http://yiianswers.com/url-management/how-to-setup-urlmanager-to-make-url-more-friendly-for-seo/ In above URL my config tell url-management is controller and how-to-setup-urlmanager-* tells this is action. But problem is function actionHow-to-setup-urlmanager(){} this is not valid. So, please help me again Thanks

      By Hrishita · 608 days 6 hours 57 mins ago

  2. 0 votes

    Hello Hrishita

    1st – try to post an answer and use the codeblock button when submitting code. Easier to read. ;)

    2nd – I think the problem now is not with urlManager but with controller definition.

    Another example, using your example URL

    Suppose you want the following link to work.

    http://yourdomain.com/url-management/how-to-setup-urlmanager-to-make-url-more-friendly-for-seo/

    In your controller.

    1. class SiteController extends Controller
    2. {
    3. ...
    4. // accessRules must be set
    5.  
    6.  
    7. public function actionWhatever() {
    8. ...
    9. }
    10.  
    11. }
    class SiteController extends Controller
    {
    ...
    // accessRules must be set 
    
    
    public function actionWhatever() {
    ...
    }
    
    }

    In config/main.php

    1. // uncomment the following to enable URLs in path-format
    2.         'urlManager'=>array(
    3.             'urlFormat'=>'path',
    4.             'showScriptName' => false,
    5.             'rules'=>array(
    6.                 '/url-management/how-to-setup-urlmanager-to-make-url-more-friendly-for-seo/' => array('site/whatever'),
    7.                 '/'=>'/view',
    8.                 '//'=>'/',
    9.                 '/'=>'/',
    10.                 //'~'site/page'
    11.             ),
    // uncomment the following to enable URLs in path-format
            'urlManager'=>array(
                'urlFormat'=>'path',
                'showScriptName' => false,
                'rules'=>array(
                    '/url-management/how-to-setup-urlmanager-to-make-url-more-friendly-for-seo/' => array('site/whatever'),
                    '/'=>'/view',
                    '//'=>'/',
                    '/'=>'/',
                    //'~'site/page'
                ),

    This is “hardcoded” way of doing it.

    If you want to create URL based on ID’s or Name’s you will need regular expression.

    Please look over previous links.

    Hope this helps

    By tuolden · 607 days 20 hours 37 mins ago

    Questions: 25 Accepted: 0 ( 0% ) | Reputation: 0

  3. 0 votes

    The default urlManager that is commented works fine for me simple by uncommenting it…

    1.                 // uncomment the following to enable URLs in path-format
    2.                 'urlManager'=>array(
    3.                         'urlFormat'=>'path',
    4.                         'showScriptName'=>false,
    5.                         'rules'=>array(
    6.                                 '<controller:\w+>/<id:\d+>'=>'<controller>/view',
    7.                                 '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
    8.                                 '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
    9.                         ),
    10.                 ),
                    // uncomment the following to enable URLs in path-format
                    'urlManager'=>array(
                            'urlFormat'=>'path',
                            'showScriptName'=>false,
                            'rules'=>array(
                                    '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                                    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                                    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                            ),
                    ),

    Then I just use the following .htaccess in the same directory as my index.php file:

    1. RewriteEngine On
    2. RewriteBase /
    3. RewriteCond %{REQUEST_FILENAME} !-f
    4. RewriteCond %{REQUEST_FILENAME} !-d
    5. RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]

    If I use Yii in a subdirectory path like http://www.myserver.com/myapp/ I change my .htaccess as follows:

    1. RewriteEngine On
    2. RewriteBase /myapp/ #Must reflect the Document Root relative path!
    3. RewriteCond %{REQUEST_FILENAME} !-f
    4. RewriteCond %{REQUEST_FILENAME} !-d
    5. RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
    RewriteEngine On
    RewriteBase /myapp/ #Must reflect the Document Root relative path!
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]

    One last item, please also check and make sure that mod_rewrite is enabled in apache by uncommenting the line:

    1. LoadModule rewrite_module modules/mod_rewrite.so
    LoadModule rewrite_module modules/mod_rewrite.so

    By ddreggors · 607 days 18 hours 6 mins ago

    Questions: 0 Accepted: 0 ( 0% ) | Reputation: 0

  4. 0 votes

    Ahh yes, one more thing…

    Note that my urlManager has the line:

    1. 'showScriptName'=>false,
    'showScriptName'=>false,

    That will make sure your auto-generated links (CHtml::link or zii.widgets.CMenu) do not have index.php in them. This will not work without the .htaccess file. The reason is that the .htaccess file rewrites the url to point to index.php/$1 so that Yii actually gets the request properly. You will not see the index.php in the url at any time though.

    By ddreggors · 607 days 18 hours ago

    Questions: 0 Accepted: 0 ( 0% ) | Reputation: 0

© YiiAnswers.com 2011. All rights reserved.

Switch to our mobile site