Please help with Friendly URLs


(Ndemson) #1

I am triying to set friendly URL for a domain but it seems I am doing someting wrong,
can anyone please help?
here is the content of my urlManager array in the config/web.php
‘urlManager’ => [
‘enablePrettyUrl’ => true,
‘showScriptName’ => false,
‘rules’ => [
http://www.domain.com/men’ => ‘?category=3’,
http://www.domain.com/women’ => ‘?category=2’,
],
]


(Johnson) #2

Hi There,

Your actual requirement is to have URL friendly or for some specific url rewrite?
Then just do it in your .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

Then in your config/web.php change the URL Mananger

'urlManager' => [
    'class' => 'yii\web\UrlManager',
    // Disable index.php
    'showScriptName' => false,
    // Disable r= routes
    'enablePrettyUrl' => true,
    'rules' => array(
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ),
],

(Ndemson) #3

Hi Johnson,

The problem is that i want to be able to choose any friendly name independently from the controller names and actions, is that possible?
Or maybe I am not getting you well?


(Johnson) #4

Hi There,

Yes it is possible!.. It’s called as Run-time Routing!. For more information please go ahead to read here…

https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing


(Ndemson) #5

Thanks for this,
but one more thing please:
Doing like this works fine

'rules' => [
    'http://admin.example.com/login' => 'admin/user/login',
    'http://www.example.com/login' => 'site/login',
]

But not when i add an additional GET parameter like this for example ‘http://www.example.com/login’ => ‘site/login?u=10’,
Any hint on that? how can I pass additional parameter without having to modify my action method or add a parameter to the action method?


(Ndemson) #6

Anymore help?
I stock with the fact that I cannot pass GET parameters on the url like this:

'rules' => [
    'http://admin.example.com/login' => 'admin/user/login',
    'http://www.example.com/login' => 'site/login?u=123',
]

the first entry in the rules array works but not the second one
Thanks


(stitch) #7

Hi, try this:

'rules' => [
    'http://admin.example.com/login' => 'admin/user/login',
    'http://www.example.com/login/<u:\d>' => 'site/login',
]

And your url will be looks like: http://www.example.com/login/123
To create url:

$url = Url::to(['site/login', 'u' => 123]);

Your action in SiteController must have the next signature:

publc function actionLogin($u)
{
   // some logic
}

(Ndemson) #8

Thank you Guys,
I finally got it work with the first solution of Johnson:

'rules' => array(
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ),

thanks again