Url rules for nested modules

I have the following url rules defined in config:

'modulename/< code>/< module>/< controller>/< action> ’ => ‘modulename/< module>/< controller>/< action>’
‘modulename/< code>/< controller>/< action>’ => ‘modulename/< controller>/< action>’

(space added show it shows up)

< code> is the parameter passed in the action function. The nested module is defined in config file.

When trying to access path /modulename/module/controller/action?code=abc, it’s expected to match the first rule. However when looking in Router section of debug, it fails to match the first rule and uses the second rule, which results in 404 error because controller with the nested module name doesn’t exist.

If < code> is removed from the rule, it works as intended.

How can this be fixed?

The whole purpose of having the rules is to use them. Does it work when you request