【问题标题】:YII URL how to generate new URLYII URL如何生成新的URL
【发布时间】:2014-06-23 02:22:50
【问题描述】:

我有一个网站,我想要像SiteURL/about.html. 这样的网址

我已经更改了config/main.php 中的代码,但是当我登录到站点时,当我导航到任何其他页面时,会话就会过期。

如果在config/main.php 文件中注释此代码'showScriptName'=>false,那么它工作正常,现在我的登录会话被保存并且我们没有过期但是现在我的URL 变成了这样SiteURL/index.php/about.html

但我想要像about.html.这样的网址

我还有另一个用于游戏选项的控制器,其 URL 类似于:http://www.demo.com/index.php/Games/info/9.html,但我希望 URL 像 SiteURL/black-hook.html 在这个“black-hook”是游戏的名称。

'urlManager'=>array( 
          'urlFormat'=>'path', 
          'showScriptName'=>FALSE, 
          'rules'=>array(
                 '<action>'=>'site/<action>',
                 '<view>'=>'array('site/page')',
                 '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                 '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                 '<controller:\w+>/<action:\w+>'=>'<controller>/<action>'
           ), 
     ),

*SiteURL = http://www.demo.com

【问题讨论】:

  • 请发布您的 .htaccess。
  • 顺便说一句。为什么是“.html”??? Yii 是基于 PHP 的!

标签: php yii url-rewriting yii-routing


【解决方案1】:

首先要将 .html 附加到您的页面,您需要在 urlManager 中使用假 url 后缀选项

这可以通过像这样将'urlSuffix' =&gt;'.html' 添加到您的urlManager 配置来完成

'urlManager'=>array( 
          'urlFormat'=>'path', 
          'showScriptName'=>false, 
           'urlSuffix'=>'.html',
          'rules'=>array(
                 '<action>'=>'site/<action>',
                 '<view>'=>'site/page',
                 '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                 '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                 '<controller:\w+>/<action:\w+>'=>'<controller>/<action>'
           ),
     ),

其次,为了在 URL 中隐藏 index.php(称为入口脚本),除了在此处更改配置外,您还需要更改 apache/nginx 服务器中的配置。

使用以下内容创建文件 /.htaccess 并保存

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

接下来验证 AllowOveride All 是否在您的 apache 配置文件中用于您的 app 目录您也可以使用 Directory 元素将上述配置直接添加到您的 apache 配置文件中,而无需使用 .htaccess 。 注意:为此,您需要在 apache 配置中启用重写引擎,您可以使用以下命令集来实现

a2enmod rewrite

最后重启apache2

/etc/init.d/apache2 restart

或

service apache2 restart

【讨论】:

  • 嗨,谢谢你,我已经尝试过了,但有时用户变成会话注销,用户无法登录网站,你能帮我解决一下我这里有什么遗漏吗?跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
  • 2018-10-01
  • 2022-11-19
  • 1970-01-01
  • 1970-01-01
  • 2014-09-20
  • 1970-01-01
相关资源
最近更新 更多