问题
I am quoting the guide:
Depending on the urlManager configuration, the created URL may look like one of the following (or other format). And if the created URL is requested later, it will still be parsed back into the original route and query parameter value.
/index.php?r=post/view&id=100
/index.php/post/100
/posts/100
I am interested in third option or second without index.php. But how can I get it ? I have .htaccess that is removing index.php ( I may need something for nginx, but do not know what and how, I have never used it). And I have pretty url set:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
what is next ?
I have asked the same question on yii2 forum, but I got no answer :(
回答1:
You don't need to change anything in .htaccess
or Nginx
for this purposes.
index.php
already removed with that setting:
'showScriptName' => false,
Pretty urls are already enabled with that line:
'enablePrettyUrl' => true,
That means urls like ?r=post/view
won't be generated.
The only thing left is to configure rules
.
If we have PostController
and its action view
and id
as primary key, second option without index.php
will be:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'post/<id:\d+>' => 'post/view',
],
],
For the third option just add s
in the left section of rule:
'posts/<id:\d+>' => 'post/view',
The official documentation covers that here.
回答2:
In order to remove /web prefix you need to point your webserver to this subdirectory instead of the project root
来源:https://stackoverflow.com/questions/27944703/how-to-get-clean-urls-in-yii2-like-post-100