Mod Rewrite With Curlmanagement

Is there any possibility to get the following example:

So I have to set the CUrlManagement rules dynamically, but how?

My first guess was: try to set UrlManagement rules in the controller or model, but I found this thread: http://www.yiiframework.com/forum/index.php/topic/21884-module-and-url-management/ which says, that it isnt possible in yii 1.x

You have found the answer to another question.

In your case all you have to do is

  1. create .htaccess file if there’s none yet

  2. add ‘urlFormat’=>‘path’ to UrlManager config (documentation here)

  3. decide how your app should distinguish controller names from Item names.

But I have to declare the rules for every Itemname anyway?, to create the redirect to index.php?r=item/index?name=Itemname

Sorry, I still dont get it…

Depends on how many items do you have, and what are their names.

Look, you need to tell Yii that in this case it’s a item, and in that case it’s a controller name.

If you don’t, then Yii is not able to tell apart controller name and item name.

There are a lot of ways of doing it, and if you use forum search you’d probably find some examples, because this question is very frequently asked.

My favourite way is using suffixes:

/something.html = item/index?name=something (as param name)

/something = something/index (as controller name)

Other ways are:

  • Listing all available controller names in the rule. That is, “if param doesn’t look like one of known controller names then it’s a param for item/index”

  • Modifying default routing rules so that all params starting with, say, ‘c_’ are controller names. All the rest is params.

That is, “if param equals to c_…, then it’s a controller name, otherwise is a param”.

  • Using virtual ‘folders’, like ‘/c/something’ = controller, “/something” is a param.

  • … and many more

You’ll probably find many good examples here

I already understand the way how it could work but the Items are user created items, so they could be a few thousand (but they are unique though), I just store them in my DB and want to get a Detailview of these items with example.com/Itemname.

The major problem is, I should declare the rule in my config/main like u said: "/something.html = item/index?name=something (as param name)", but I have no idea how I can declare them automatically

Let me give you some examples.


'<controller:site|news|...|user>/<action:\w+>' => '<controller>/<action>', // this rule intercepts all the controllers you have

'<name:\w+>' => 'item/index' // this rule will activate if passed param doesn't look like controller name

Bad thing is you cannot have items named like controllers in this case.


'<name:\w+>\.html' => 'item/index'

In this case all urls like /something.html, /another.html will be processed by item/index.

So, some things to remember:

  1. URL rules are just old plain regexps

  2. Rules are tested from the top

  3. Parser will stop when it finds matching rule.

That’s why you should place more specific rules above less specific ones.

well tried it already before like:


'urlManager'=>array(

      'urlFormat'=>'path',

      'rules'=>array(

      '<name:\w+>\.html' => 'item/create'

    ),

  ),

but I am getting a 404 apache error, thats the problem I had…

Do you have .htaccess file?

What URL you’re requesting?

yes, my .htaccess is located in protected/.htaccess and looks like:


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

Rquested URL:


localhost/project/itemname

Also tested it with the example of the CUrlManager:


'item' => 'item/index'

and it still doesnt work…

Are you sure mod_rewrite is on?

Try to remove all you custom rules and access action directly.

In your case, I suppose, it’s something liks http://localhost/project/item/index

or you can use some existing controllers, like /contact

If 404 is still there, then check that mod_rewrite is active and AllowOverride for this dir is not None.

alright, it works fine for me.

But got a new problem.

example.com/samsung -> works

example.com/samsung-galaxy-> doesnt work

Error code:




Error 404

Unable to resolve the request "samsung-galaxy".

I guess the problem is because of the hyphen. Do I have to modify the rule somehow?

The rules:


'rules'=>array(

    '<controller:(site|item|login|register|user)>/<action:\w+>' => '<controller>/<action>', 

    '<itemurl:\w+>' => 'item/details' 

)

Sure. Just modify it the way it can match all the needed routes.

For example, \w+ ===> [\w\-_]+