如何在yii框架中隐藏我的url中的index.php

时间:2016-10-26 10:14:43

标签: php .htaccess yii

我的代码使用url运行:

http://localhost/yii/index.php/Adminlogin

我希望网址看起来像:

http://localhost/yii/Adminlogin

.htaccess文件

Options +FollowSymLinks
IndexIgnore */*
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

main.php

'urlManager'=>array(
        'urlFormat'=>'path',
        'showScriptName'=>false,
        'rules'=>array(
            'redirect/<redirectUrl>'=>'site/index',
            'login'=>'site/login',
            'privacy'=>'site/privacy',
            'password'=>'site/forgot',
            '<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
        ),
    ),

5 个答案:

答案 0 :(得分:1)

RewriteRule ^ index.php [L]代替RewriteRule . index.php

答案 1 :(得分:0)

在main.php

中使用此功能
'urlFormat'=>'path',
'showScriptName'=>false,

在下面的htaccess使用中,

RewriteRule ^(.+)$ index.php?$1 [PT,L,QSA]

答案 2 :(得分:0)

试试这个

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php(.*) [NC]
RewriteRule ^(.*)index\.php(.*)$ http://%{HTTP_HOST}$1/$2 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php(.*) [NC]
RewriteRule ^index\.php(.*)$ http://%{HTTP_HOST}$1 [R=301,L]

答案 3 :(得分:0)

另外,请记住主apache配置中的AllowOverride

<Directory "/path/to/your/yii.sample/web/">
    Options FollowSymLinks Multiviews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
    allow from all
</Directory>

你的web/.htaccess是否正确,但你的apache从未允许你的

答案 4 :(得分:0)

我的htaccess看起来像

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

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