How to remove index.php in Yii Framework

2019-01-17 12:38发布

问题:

Hey 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

UPDATE:

This is the code in my .htaccess file:

# 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

回答1:

Try this

A good description is available here

http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x



回答2:

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

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


回答3:

taken directly from yii documentation

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.

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.

Hope this helps, because solved my issue as well.



回答4:

Do these 3 steps:

  1. Enable Url re-writing on Apache.

  2. Place .htaccess on root of your project

RewriteEngine on

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


回答5:

change your config/main.php like below:

'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

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/$1 [L,QSA]

done...hope it will work



回答6:

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):

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


回答7:

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 .htaccess file to protected directory also, then it worked.



回答8:

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



回答9:

modify apache config values

AllowOverride none to AllowOverride ALL

in httpd.conf or httpd-vhosts.conf



标签: php url yii