如何生成新的URL



我有一个网站,我想在其中的URL像SiteURL/about.html.

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

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

但是我想要的URL是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

首先要获得。html附加到您的页面,您需要在urlManager

中使用假url后缀选项

这可以通过将'urlSuffix' =>'.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

最新更新