【问题标题】:How to make clean URLs in Yii2 framework using .htaccess?如何使用 .htaccess 在 Yii2 框架中制作干净的 URL?
【发布时间】:2016-03-22 17:25:26
【问题描述】:

我正在尝试为使用 Yii2 框架开发的网站创建干净的 url,仅使用 .htaccess。我知道我可以使用 Yii2 的 urlmanager 类。但由于某些特定原因,我无法使用它,只想使用 .htaccess 制作干净的 url。

我想转换以下网址: 网址 = 'http://localhost:8090/index.php?r=site%2Fcontact' 至 网址 = 'http://localhost:8090/contact'

提前致谢。

【问题讨论】:

  • 从未尝试过这样做,但您只想要一个控制器?
  • 不,我想对所有控制器都这样做。
  • 那么你显然需要它是 localhost:8090/site/contact。但只要你不能编辑 yii 文件,这不是 yii 问题。尝试阅读 apache .htaccess 文档。

标签: php apache .htaccess mod-rewrite yii2


【解决方案1】:

首先你必须在你的 yii2 应用配置中更改 urlManager 组件:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
]

showScriptName 将从 URL 格式中删除 index.php

enablePrettyUrl 会提供漂亮的 URL 格式,比如你想要的“http://localhost/contant”)

其次,您需要在主机配置中设置 URL 重写规则。 .htaccess (Apache) 示例:

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

您可以在Yii2 documentation阅读更多内容

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 2014-12-18
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多