如何在 Yii 框架中删除 index.php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9633649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:19:05  来源:igfitidea点击:

How to remove index.php in Yii Framework

phpurlyii

提问by Code Prank

enter image description hereHey guyz i am a newbie in Yii framework. I want to remove the index.php from my urls. Following the yii documentation when i put the rewrite engine code in my .htaccess file and setting showScriptName to false in my config/main.php file i get the 500 internal server error. My .htaccess file is located in root folder of my application. Tell me where i am doing wrong

在此处输入图片说明嘿 Guyz 我是 Yii 框架的新手。我想从我的网址中删除 index.php。按照 yii 文档,当我将重写引擎代码放入我的 .htaccess 文件并在我的 config/main.php 文件中将 showScriptName 设置为 false 时,我收到 500 内部服务器错误。我的 .htaccess 文件位于我的应用程序的根文件夹中。告诉我哪里做错了

UPDATE:

更新:

This is the code in my .htaccess file:

这是我的 .htaccess 文件中的代码:

# 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

回答by Akhil Thayyil

回答by Samuel Liew

If you also want to remove /indexfrom the main page URL, add ''=>'site/index'to the top of your urlManager rules, like so:

如果您还想/index从主页 URL 中删除,请添加''=>'site/index'到 urlManager 规则的顶部,如下所示:

'urlManager'=>array(
    'urlFormat'=>'path',
    'showScriptName' => false,
    'rules'=>array(
        ''=>'site/index',
        '<action>'=>'site/<action>',

回答by WebDevPT

taken directly from yii documentation

直接取自 yii 文档

Hiding index.php There is one more thing that we can do to further clean our URLs, i.e., hiding the entry script index.php in the URL. This requires us to configure the Web server as well as the urlManager application component.

We first need to configure the Web server so that a URL without the entry script can still be handled by the entry script. For Apache HTTP server, this can be done by turning on the URL rewriting engine and specifying some rewriting rules. We can create the file /wwwroot/blog/.htaccess with the following content. Note that the same content can also be put in the Apache configuration file within the Directory element for /wwwroot/blog.

隐藏 index.php 我们还可以做一件事来进一步清理我们的 URL,即将入口脚本 index.php 隐藏在 URL 中。这需要我们配置 Web 服务器以及 urlManager 应用程序组件。

我们首先需要配置 Web 服务器,以便没有入口脚本的 URL 仍然可以由入口脚本处理。对于 Apache HTTP 服务器,这可以通过打开 URL 重写引擎并指定一些重写规则来完成。我们可以使用以下内容创建文件 /wwwroot/blog/.htaccess。请注意,同样的内容也可以放在 /wwwroot/blog 的 Directory 元素内的 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

We then configure the showScriptName property of the urlManager component to be false.

Now if we call $this->createUrl('post/read',array('id'=>100)), we would obtain the URL /post/100. More importantly, this URL can be properly recognized by our Web application.

然后我们将 urlManager 组件的 showScriptName 属性配置为 false。

现在,如果我们调用 $this->createUrl('post/read',array('id'=>100)),我们将获得 URL /post/100。更重要的是,这个 URL 可以被我们的 Web 应用程序正确识别。

Hope this helps, because solved my issue as well.

希望这会有所帮助,因为也解决了我的问题。

回答by Waqar Alamgir

Do these 3 steps:

做这3个步骤:

  1. Enable Url re-writing on Apache.

  2. Place .htaccess on root of your project

  1. 在 Apache 上启用 Url 重写。

  2. 将 .htaccess 放在项目的根目录上

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

重写引擎开启

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php
  1. In your configuration protected/config/main.php set showScriptName to false like this to your url manager components >> urlManager
  1. 在您的配置 protected/config/main.php 中将 showScriptName 设置为 false 像这样到您的 url 管理器组件 >> urlManager
'urlManager'=>array(
  'urlFormat'=>'path',
  'rules'=>array(
      '<controller:\w+>/<id:\d+>'=>'<controller>/view',
      '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
      '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
  ),
  'showScriptName'=>false,
)
'urlManager'=>array(
  'urlFormat'=>'path',
  'rules'=>array(
      '<controller:\w+>/<id:\d+>'=>'<controller>/view',
      '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
      '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
  ),
  'showScriptName'=>false,
)

回答by Ankit Rathi

change your config/main.php like below:

更改您的 config/main.php 如下:

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

then place your htaccess file on base directory with protected folder (it may be inside protected folder) place below code to your htaccess file

然后将您的 htaccess 文件放在带有受保护文件夹的基本目录中(它可能在受保护的文件夹内)将下面的代码放到您的 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

or try with this---

或者试试这个---

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/ [L,QSA]

done...hope it will work

完成...希望它会起作用

回答by Ajeet Singh

Make sure that if after all your changes to .htaccess & urlManager, if it still not seems to be working to anyone of you, is that you have modified your AllowOverride parameter for root directory in Apache httpd.conf file to "All" instead of "None". After modification restart apache. Because this is the parameter which let apache decide what all parameters can be overridden in .htaccess file

确保在对 .htaccess 和 urlManager 进行所有更改之后,如果它似乎仍然对你们中的任何人不起作用,那么您是否已将 Apache httpd.conf 文件中根目录的 AllowOverride 参数修改为“全部”而不是“没有任何”。修改后重启apache。因为这是让 apache 决定所有参数可以在 .htaccess 文件中覆盖的参数

回答by Ganesan Murugesan

modify apache config values

修改 apache 配置值

AllowOverride none to AllowOverride ALL

AllowOverride none 到 AllowOverride ALL

in httpd.conf or httpd-vhosts.conf

在 httpd.conf 或 httpd-vhosts.conf 中

回答by Sarvar Nishonboev

I had also this problem today when I have shifted my project to linux server. I did all ways that are shown, but not helped. Then, I have copied .htaccessfile to protecteddirectory also, then it worked.

今天当我将项目转移到 linux 服务器时,我也遇到了这个问题。我做了所有显示的方法,但没有帮助。然后,我也将.htaccess文件复制到了受保护的目录,然后它就起作用了。

回答by Xoundboy

I just came across this thread but found that in order to get it working I not only had to follow the instrucitons for adding the .htaccess file but also had to uncomment the section below from my main config file (\protected\config\main.php):

我刚刚遇到这个线程,但发现为了让它工作,我不仅必须按照添加 .htaccess 文件的说明进行操作,而且还必须从我的主配置文件 (\protected\config\main.htaccess) 中取消注释以下部分。 php):

        // uncomment the following to enable URLs in path-format

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