How to create a pretty URL?

[color=#222222][size=2]How can I create a pretty url like this customer/index/amount/12000/location/in . In Yii1.1 it was available by default. it was easy to enable pretty url. In Yii2 if need pretty url I’ve to write rules for every action !!![/size][/color]

[color=#222222][size=2]In Codeigniter you will get index.php/controller/action/parameter1/parameter2/parameter3 ie it does not expose action parameter variables, that too without writing any url rules![/size][/color]

Hi!

Read "Using Pretty URLs":

http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html

Regards

thanks

Try Encrypting Your URLs… makes more sense that way. You can create class for that

[color="#222222"]Below is my main.php[/color]




   	'urlManager' => [

        'enablePrettyUrl' => true,

        'showScriptName' => false,

        'rules' => [  '<controller:\w+>/<id:\d+>' => '<controller>/view',

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

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

                    ],

        ],



[color="#000000"][font="arial, verdana, tahoma, sans-serif"] [/font][/color]

[size=“2”]I have enabled pretty url (i think), i am getting a lot of 404’s, like[/size]

[size="2"]192.168.1.3/~user/urshow/frontend/web/movies/movies_all it would have work fine if it would be like this 192.168.1.3/~user/urshow/frontend/web/index.php?r=movies/movies_all and no links are working which worked perfect previously.[/size]

Hi!

For me it works simply like this:




        # Setup URL Manager for Path URLS

        'urlManager' => [

            'enablePrettyUrl'       => true,

            'showScriptName'        => false,

            'enableStrictParsing'   => false,

            'rules' => [

                // i only use default rules so this is empty

            ],

        ],



And create file app/web/.htaccess




RewriteEngine on

 

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php



And now I can access pages for exmaple like:

localhost/myproject/web/site/about

Regards

1 Like

No such thing as "pretty" URLs in Yii2… it encodes them all for some unexplained reason.

eg, I just did an update and I get this:




index.php?r=account%2Faddress



instead of this:




index.php?r=account/address



A REAL pretty URL is this:




/address.html



Has anybody managed to do something like the latter?

Hi!

Yes you can.

As described here:

http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html

You can add the .html with "suffix" attribute in URL Manager.

Like:




        'urlManager' => [

            'enablePrettyUrl'       => true,

            'showScriptName'        => false,

            'enableStrictParsing'   => false,

            'suffix'                => '.html', // here

            'rules' => [

                // ...

            ],

        ],



Other / different / multiple suffixes are also possible.

Just read "URL Suffixes" from the link above.

As far as I understood, you can also make your URLs even "prettier" with URL rules.

So that for example myproject.local/users/profile/view?id=4 becomes myproject.local/userprofile.html?id=4 or something like that.

But I have not enough experience with URL rules so far to give advice on this.

For my lan-only used applications I had no need to figure this out so far.

Regards