如何获得干净的URL

如何获得干净的URL

本文介绍了如何获得干净的URL yii2像``交/ 100``的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我引述指南:

根据urlManager配置,创建的URL可能看起来像下面的(或其它格式)之一。如果所创建的URL被购买请求,它仍然会被解析成原来的路线和查询参数值

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

我感兴趣的第三个选项或第二没有index.php文件。但是,我怎么能得到它?我的.htaccess被删除的index.php(我可能需要的东西nginx的,但不知道是什么,以及如何,我从来没有使用过)。我有pretty的URL集:

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 ?

我也问过相同问题上yii2论坛,但我没有得到答案:(

I have asked the same question on yii2 forum, but I got no answer :(

推荐答案

您不必更改的.htaccess 或任何 Nginx的此目的。

You don't need to change anything in .htaccess or Nginx for this purposes.

的index.php 与设置已删除:

'showScriptName' => false,

pretty的网址已经与该行启用:

Pretty urls are already enabled with that line:

'enablePrettyUrl' => true,

这意味着像?R =交/浏览网址将不会生成。

That means urls like ?r=post/view won't be generated.

剩下的唯一一件事就是配置规则

The only thing left is to configure rules.

如果我们有 PostController中及其行动视图 ID 作为主键,第二个选项无的index.php 将是:

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',

有关的正式文件涵盖这里

这篇关于如何获得干净的URL yii2像``交/ 100``的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:42