# General Regex For Url Routing

(Julian) #1

I have an issue with URL routing and am not sure if Yii is capable of achieving what I want. We have users coming in on old (and malformed) urls such as:



/this-is-a-page&pgst=100

/this-is-a-page&ref=547

/this-is-a-page&ref=443



I currently have a rule in the urlManager config as such:


'this-is-a-page' => 'contentPage/index/src/37'



which works fine for visits to:


/this-is-a-page



but not for


this-is-a-page&pgst=100



etc.

Is it possible to write general rule to capture these cases? I have tried:


'this-is-a-page*' => 'contentPage/index/src/37'



&


'this-is-a-page<\w+>' => 'contentPage/index/src/37'



But they do not seem to work as I would expect.

Thanks for any help

(Kburton) #2

If you don’t need to capture the extra parameters, try:



'this-is-a-page.*' => 'contentPage/index/src/37'



or



'this-is-a-page<.*>' => 'contentPage/index/src/37'



I think the first is the correct one.

(Julian) #3

We don’t need the extra params, unfortunately neither of those seem to work. We are using:


'urlFormat'=>'path'



Will that make the solution any different?

This is my urlManager code in full:



'urlManager'=>array(

'urlFormat'=>'path',

'rules'=>array(

'this-is-a-page<.*>' => 'contentPage/index/src/37',

),

),



going to url:


/this-is-a-page



and getting the error:


Unable to resolve the request "this-is-a-page"



(Kburton) #4

Make sure you haven’t got another entry for urlManager defined in your config array.

(Julian) #5

That is the only entry for urlManager in the config array

(Kburton) #6

Try this:



'this-is-a-page<rest:.*>' => 'contentPage/index/src/37',



(Julian) #7

Great, that worked! Thanks

(Vishwas Jagetiya) #8

I have to do the following generate a

$v2ApiList = array( ‘abc’, ‘def’ );$v2Regex = ‘(’;

foreach ($v2ApiList as$index=>$v2Api) { if($index>0)
{
$v2Regex =$v2Regex . ‘|’;
}
$v2Regex =$v2Regex . ‘\b’ . $v2Api . ‘\b’; }$v2Regex = $v2Regex . ‘)’; ‘rules’ => array( ‘service/v2/<controller:(?!’ .$v2ApiRegex . ‘)\w+>/<action:\w+>’ => ‘service/<controller>/<action>’,

‘service/v2/<controller:’ . \$v2ApiRegex . ‘>/<action:\w+>’ => ‘service/v2/v2<controller>/<action>’)

this is to generate

‘service/v2/<controller:(?!(\banc\b|\bdef\b=))\w+>/<action:\w+>’ => ‘service/<controller>/<action>’,

and

‘service/v2/<controller:(\babc\b|\def\b)>/<action:\w+>’ => ‘service/v2/v2<controller>/<action>’

http://172.16.5.153:3000/index.php/service/v2/ab/de — this is going to

‘service/v2/<controller:(\babc\b|\def\b)>/<action:\w+>’ => ‘service/v2/v2<controller>/<action>’