简体   繁体   English

如何在apache / .htaccess / mod_rewrite中隐藏yii的index.php文件?

[英]How can I hide the index.php file for yii in apache/.htaccess/mod_rewrite?

I have this htaccess file: 我有这个htaccess文件:

RewriteEngine on
#if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

And I'm using Yii 1.1.15 ( this site was taken from 1.1.13 version but such version is not available anymore for download, so I'm using it in a 1.1.15 framework install - althought it has no problem as far as I can see), having a conf/main.php file like: 我正在使用Yii 1.1.15( 此站点取自1.1.13版本,但该版本不再提供下载,因此我在1.1.15框架安装中使用了它-尽管到目前为止它没有问题如我所见),拥有conf / main.php文件,例如:

<?php

return array(
    'basePath'=>dirname(dirname(__FILE__)),
    'name'=>'Grand Duval - Códigos premiados',
    'defaultController'=>'participantes/create',
    'language'=>'es',

    // preloading 'log' component
    'preload'=>array('log'),

    // autoloading model and component classes
    'import'=>array(
        'application.models.*',
        'application.components.*',
    ),

    'modules'=>array(
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>'mypassword',
            'ipFilters'=>array('127.0.0.1', '::1'),
        ),
    ),

    // application components
    'components'=>array(
        'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=>true,
        ),

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

        'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=mydatabase',
            'emulatePrepare' => true,
            'username' => 'sbuser',
            'password' => 'dbpassword',
            'charset' => 'utf8',
        ),

        'errorHandler'=>array(
            'errorAction'=>'site/error',
        ),
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning',
                ),
            ),
        ),
    ),

    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params'=>array(
        // this is used in contact page
        'adminEmail'=>'webmaster@example.com',
    ),
);

The important part here is: 这里的重要部分是:

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

Such config is intended - and does work like that - to generate absolute urls like http://www.mydomain.dev/controller/action for controller controller and action action . 这样的配置旨在并确实像那样工作,以生成用于控制器controller和action action绝对URL,例如http://www.mydomain.dev/controller/action

I have the problem with the .htaccess file: If I try to hit http://www.mydomain.dev/controller/action , the url is not found. 我的.htaccess文件有问题:如果我尝试打http://www.mydomain.dev/controller/action ,则找不到URL。 However if I hit http://www.mydomain.dev/index.php/controller/action it is found. 但是,如果我点击http://www.mydomain.dev/index.php/controller/action ,就会发现它。 It looks that .htacccess has no effect at all (It is the exact same if I delete the .htaccess file). 看来.htacccess根本没有任何作用(如果删除.htaccess文件,则完全一样)。

Q : what am I doing wrong? :我在做什么错?

I think you need to enable mod_rewrite try: 我认为您需要启用mod_rewrite尝试:

a2enmod rewrite

Then just restart your server. 然后重新启动服务器。

I found the answer. 我找到了答案。 After ensuring that mod_rewrite was active for my other sites I asked myself: Is my .htaccess being recognized? 在确保mod_rewrite在其他站点上处于活动状态后,我问自己:.htaccess是否可以识别?

So i wanted to force a 500 error by corrupting my .htaccess file: 所以我想通过破坏我的.htaccess文件来强制执行500错误:

RewriteEngine on
# if a directory or a file exists, use it directly
TryToForceA500
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

And no error was triggered! 并没有触发任何错误! So I asked myself -again-: Why isn't it being recognized? 所以我再次问自己:为什么不被认可? and checked my site .conf file and found: 检查了我的站点.conf文件 ,发现:

AllowOverride none

And changed to: 并更改为:

AllowOverride all

(this is just a dev server, so that setup is fine for me) (这只是一个开发服务器,因此设置对我来说很好)

And then the file was recognized and worked! 然后文件被识别并工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM