Url Manager Rules

Hi,

Im new in Yii and Im trying to create an application that needs to parse the URLs on a special way. as follows:

if the first part of the URL isn’t a valid controller and the second part isn’t a valid action, then, calls site/pages, passing as parameters the frist and second parts as enterprise and area respetivelly.

In the site/pages action I’ll try to find a page content on the database with the enterprise and area parameters passed. If nothing was found, then redirec the application to not found page.

I would like to do that to reduce front pages URLs to be only www.mysite.com/enterpriseName/areaContentName.

Does that can be done?

I tried that

‘urlManager’=>array(

'urlFormat'=>'path',


'rules'=>array(


	'<enterprise:(^(controller))>/<area:(^(action))>'=>'site/pages',


	'<controller:\w+>/<id:\d+>'=>'<controller>/view',


	'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',


	'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',


),

),

thanks in advance and sorry about the terrible english.

This is from Larry Ulman’s Yii book. I highly recommend buying it for $20 online. http://yii.larryullman.com/

Hiding the Index File If you want to take your URL customization

further, it’s possible to configure “urlManager”, along with an Apache

.htaccess file, so that index.php no longer needs to be part of the URL:

http://www.example.com/ControllerID/ActionID/.

To do this, you have to add a mod_rewrite rule to an .htaccess file stored in your

Web root directory (i.e., in the same directory as index.php). The contents of that

file should be:

<ifModule mod_rewrite.c>

Turn on the engine:

RewriteEngine on

Don’t perform redirects for files and directories that exist:

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

For everything else, redirect to index.php:

RewriteRule ^(.*)$ index.php/$1

</ifModule>

If you’re not familiar with mod_rewrite, you can look up oodles of tutorials

online. Note that this modification will only work if your Web server allows for

configuration overrides using .htaccess. If not, then you may be a bit over your head

anyway. You can either look online for how to allow for overrides via .htaccess,

skip this step for now, or ask for help in my support forums.

Once you’ve implemented the mod_rewrite rules, to test if mod_rewrite is

working, go to any other file in the Web directory (e.g., an image or your CSS

script) to see if that loads. Then go to a URL for something that doesn’t exist (e.g.,

www.example.com/varmit) and see if the contents of the index page are shown

instead (most likely with an error message).

Finally, you must tell the URL manager not to show the bootstrap file by setting the

showScriptName property to false:

56




'urlManager'=>array(

'showScriptName'=>false,

'urlFormat'=>'path',

'rules'=>array(

'<controller:\w+>/<id:\d+>'=>'<controller>/view',

'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

),



Hi Larry, I don’t think you read the question?

Fabio, have you tried putting it at the end, like this:


'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>',

    '<enterprise:\w+>/<area:\w+>'=>'site/pages',

  ),

),

That wouldn’t work, <controller:\w+>/<action:\w+> would match first and throw a 404 because the controller doesn’t exist.

Instead you should move the code of your site/pages action to a custom URL rule class. The example literally describes what you want to do.

Worked like a charm.

Thanks

Dear All,

I like to know what is the meaning for w+, d+ and etc in url manager.

Please explain

Thanks