How can I hide the index.php file for yii in apach

2019-08-14 08:34发布

I have this htaccess file:

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:

<?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.

I have the problem with the .htaccess file: If I try to hit http://www.mydomain.dev/controller/action, the url is not found. However if I hit http://www.mydomain.dev/index.php/controller/action it is found. It looks that .htacccess has no effect at all (It is the exact same if I delete the .htaccess file).

Q: what am I doing wrong?

2条回答
劳资没心,怎么记你
2楼-- · 2019-08-14 09:12

I think you need to enable mod_rewrite try:

a2enmod rewrite

Then just restart your server.

查看更多
成全新的幸福
3楼-- · 2019-08-14 09:31

I found the answer. After ensuring that mod_rewrite was active for my other sites I asked myself: Is my .htaccess being recognized?

So i wanted to force a 500 error by corrupting my .htaccess file:

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:

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!

查看更多
登录 后发表回答