Yii how to get clean and pretty URL

2019-02-14 19:33发布

问题:

I am newbie to Yii framework. I have uncommented the url manager in the config file and got a url scheme like this:

http://localhost/mysite/index.php/displayAll

I don't want the index.php in the url. So I want a url some thing like this

http://localhost/mysite/displayAll

To accomplish this, what should I do. I did play with the url manager and some htaccess, but nothing gone well.

Please help

回答1:

Try:

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


回答2:

In your .htaccess you should have this:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Then you have to add the urlManager component to your main config file:

array(
    ......
    'components'=>array(
        ......
        'urlManager'=>array(
            'urlFormat'=>'path',
            'showScriptName'=>false,
            'rules'=>array(
                'pattern1'=>'route1',
                'pattern2'=>'route2',
                'pattern3'=>'route3',
            ),
        ),
    ),
);

Notice the 'showScriptName'=>false, this will hide 'index.php' from the generated URL.

To find everything about Yii's URL Manager check out this topic in Yii's documentation: http://www.yiiframework.com/doc/guide/1.1/en/topics.url



回答3:

In httpd.conf look for,

     LoadModule rewrite_module modules/mod_rewrite.so 

(remove # from the begining of line if not already removed.)

              OR 
     In wamp click on wampserver-> Apache -> Apache Modules and enable rewrite_module.

Create .htaccess in directory where your index.php is, and paste following code:

IndexIgnore */*
RewriteEngine on

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

RewriteRule . index.php

And in protected/config/main.php

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

If still not getting clean url,goto virtual host config file "httpd-vhosts" incase of you are using it, and paste in your config,

 <Directory "C:/wamp/www">
    AllowOverride All 
 </Directory> 


回答4:

On IIS 7, IIS 7.5, IIS 8, IIS 8.5, IIS 10

  1. Make sure you have the URL Rewrite module installed. Can be found here: http://www.iis.net/downloads/microsoft/url-rewrite

  2. remove comment from config/main.php or config/web.php

        'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
        ],
    ],
    
  3. Create new file in the web directory basic/web/web.config and add in system.webServer

    <directoryBrowse enabled="false" />
    
    <rewrite>
        <rules>
        <rule name="Hide Yii Index" stopProcessing="true">
            <match url="." ignoreCase="false" />
            <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
            </conditions>
                <action type="Rewrite" url="index.php" appendQueryString="true" />
        </rule> 
        </rules>
    </rewrite>
    

Example result: http://localhost/basic/web/site/contact