urlManager with path format and having encoded slashes as parameter

I have url manager with friendly url:s and I use htaccess to hide index.php

'urlManager'=>array(

      'urlFormat'=>'path',

      'showScriptName'=>false,

),

Now when I create url with parameter containing slashes:

$this->createUrl('site/doit', array("param"=>"/example/path"))

I get url like "http://localhost/site/doit/param/%2Fexample%2Fpath"

which apparently is not valid url for apache and I get 404 directly.

(See for example: http://www.webmaster…he/3279075.htm)

By encoding the parameters twice it is possible to get working url with %252f, but Yii isn't doing that, and I'm not even sure if thats a good workaround.

Anyway looks like, the path format doesn't work with params that has slashes, am I correct or am I just doing something wrong?

You are right that you can't have slashes in GET params when using path format. It is impossible for Yii to correctly resolve the request.

I’ve encountered this issue recently.

We can get rid of the apache 404 issues by adding


AllowEncodedSlashes NoDecode

to the apache Virtual Host (or check link for apache config and other bugs)

But like qiang says, Yii will just treat it as a forward slash which doesn’t help.

So I was wondering if there was anyway we might be able to force the urlmanager to not use path format in specific situation.

I’ve tried using Yii::app()->urlManager->urlFormat = “get”; as well as Yii::app()->urlManager->showScriptName = true; But all I ever get frome create(Absolute)Url() is something along the lines of


http://my.domain/?r=controller/action&param1=value1&param2=value2&...

which does not redirect to controller/action as it should (it will just load the site index).

If it were just a matter of creating a single url i could use createPathInfo but the issue is when you are using CGridView for example it would be nice to have an easy way to have pagination and the likes switch to "get" rather than "path" format.

Let me know if anyone has a solution to this.

1 Like

It’s easy enough to append all the named parameters you want:


'<controller:\w+>/<action:\w+>/<id:\d+>/<country>/<county>/<city>/<street>' => '<controller>/<action>',

As long as you know the parameters beforehand.

Well the problem here is that a dynamic form is used to run a search on content. This form sends the data as GET, and slashes are allowed (in say, searching dates). I do not know beforehand how many fields are going to be sent or which ones they are, but I do have to handle slashes in params.

Obviously this is an issue with ‘path’ format urls, but If I want to switch to ‘get’ format urls for this request as well as all pager links (in cgrid and clist), but keep path format for the rest of the site, I hit a bit of a wall.

I was wondering if there was anyway to switch from one to the other on demand. Just setting the urlManager->urlFormat to “get” dynamically gives me an url when I createUrl() that isn’t interpreted correctly by Yii on the other end.

Just so we’re clear:

My main.php is configured as follows:




'urlManager'=>array(

			'urlFormat'=>'path',

			'showScriptName' => false,

...



In a controller:




echo $this->createUrl('controller/action',array('paramName'=>'param/value')); // working url of /controller/action/paramName/param%2Fvalue

// but as mentioned above, Yii treats %2F as a slash so my GET is corrupt

Yii::app()->urlManager->urlFormat = 'get'; //or Yii::app()->urlManager->setUrlFormat('get');

Yii::app()->urlManager->showScriptName = true;

echo $this->createUrl('controller/action',array('paramName'=>'param/value')); //non-working ?r=controller/action&paramName=param%2Fvalue

// Yii doesn't seem to care for non-path URLs because of main.php config

Do you guys see any easy solution for this? a ‘get’ url that would work would be /controller/action?paramName=param%2FValue but the URL manager does not create urls like this oddly. I was hoping we could get another urlFormat. Is there a way of setting up a custom one? I could then hopefully configure the pager’s urlmanager options.

Hello,

I have to work with parameter values that have slashes in them and encountered the same problem.

The solution I use now is simply replacing ‘%2F’ with ‘/’ in the url returned by createUrl(). Now all parameters and values come through fine. So this works just fine for me right now.

My question is does anybody know if there is a (potential) problem with this solution?

Thanks.

How did you solve the problem?

I want to know where in the controller you put the code to?

I have the same problem. Thanks!