How to use urlmanager yii2 with controller/detail?alias=abc-xyz

70 views Asked by At

I have problem with alias on URL.

.htaccess file

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

I try use with config

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

but URL still access with link

controller/detail?alias=abc-xyz

Not access link

controller/detail/abc-xyz

I don't understand why.

please help me. Thanks,

1

There are 1 answers

1
Bizley On

Regex shorthand character \w matches [A-Za-z0-9_] so as you see there is no single - here (well, there is but for ranges only which is different thing). Read more about regex at www.regular-expressions.info.

Modify your rule like this:

'<controller:\w+>/<action:\w+>/<alias:[\w\-]+>'=>'controller/action',