如何在apache/.htaccess/mod_rewrite中隐藏yii的index.php文件? [英] How can I hide the index.php file for yii in apache/.htaccess/mod_rewrite?
问题描述
我有这个htaccess文件:
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
我正在使用Yii 1.1.15(此站点取自1.1.13 版本,但该版本不再可供下载,因此我在1.1.15中使用了该版本框架安装-尽管据我所知,它没有问题),具有conf/main.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',
),
);
这里的重要部分是:
'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>',
),
),
这种配置旨在-并像那样工作-为控制器controller
和操作action
生成绝对URL,例如http://www.mydomain.dev/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
.
我遇到了.htaccess
文件的问题:如果尝试打http://www.mydomain.dev/controller/action
,则找不到URL.但是,如果我按http://www.mydomain.dev/index.php/controller/action
键,则会找到它.看来.htacccess
根本没有任何效果(如果删除.htaccess文件,则完全相同).
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).
问:我在做什么错了?
推荐答案
我找到了答案. 确保其他网站上的mod_rewrite处于活动状态后,我问自己:.htaccess文件是否被识别?
I found the answer. After ensuring that mod_rewrite was active for my other sites I asked myself: Is my .htaccess being recognized?
所以我想通过破坏我的.htaccess文件来强制执行500错误:
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
并且没有触发任何错误!所以我再次问自己:为什么不被认可?并检查了我的网站.conf文件并发现:
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
并更改为:
AllowOverride all
(这只是一个开发服务器,因此设置对我来说很好)
(this is just a dev server, so that setup is fine for me)
然后文件被识别并工作!
And then the file was recognized and worked!
这篇关于如何在apache/.htaccess/mod_rewrite中隐藏yii的index.php文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!