2013-10-18 62 views
0

在我的测试应用程序中,我试图从url中删除index.php。我可以像http://localhost/trackstar/那样访问根页面,而不是http://localhost/trackstar/index.php,但只要我尝试访问http://localhost/trackstar/project之类的其他页面或任何其他页面,我总是会在index.php页面结束。然而开放http://localhost/trackstar/index.php/project的作品。Yii - 从url中删除index.php - 总是重定向到'index.php'

我正在使用WAMP服务器。配置如下。

启用apache中的重写模块。 配置main

'urlManager' => array(
     'urlFormat' => 'path',    
     'rules' => array(
      'commentfeed' => array('comment/feed', 'urlSuffix' => '.xml', 'caseSensitive' => false), 
      '<controller:\w+>/<id:\d+>' => '<controller>/view', 
      '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', 
      '<controller:\w+>/<action:\w+>' => '<controller>/<action>', 
     ), 
     'showScriptName' => false, 
    ), 

项目的根文件夹.htaccess

Options +FollowSymlinks 
IndexIgnore */* 
RewriteEngine On 

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

我还添加了在httpd.conf,虽然不是真的有必要,我认为:

<Directory "c:/wamp/www/TrackStar/"> 
    Options Indexes FollowSymLinks 
    AllowOverride All 
    Order allow,deny 
    allow from all 
</Directory> 

我已经在.htaccess尝试了几个额外参数的组合,但我一直在重定向到我的index.php页面。

任何想法为什么这不起作用?

编辑:

由于我被重定向,在RewriteCond检查没有成功,换句话说,REQUEST_FILENAME没有找到目录或文件。也许我的目录结构中有什么东西没有?

enter image description here

TrackStar\protected\views\project目录,例如是我试图通过http://localhost/trackstar/project访问的页面。我的Yii框架文件位于www文件夹上方的目录中。

+0

您的网址是否正确生成?或者应用程序将您重定向到index.php? – 2013-10-18 11:10:15

+0

@PeterM:我的浏览器说'http:// localhost/trackstar/project',但我在'index.php'页面。 – user729103

+0

.htaccess是否与index.php位于同一目录中? – CreatoR

回答

0

你可以尝试:在项目的根文件夹

的.htaccess:

Options +FollowSymlinks -MultiViews 

IndexIgnore */* 
DirectoryIndex index.php 

RewriteEngine On 

RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteRule ^(.*?)index\.php$ /$1 [L,NC,R=302] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . index.php [L] 
+0

谢谢,但仍然无法正常工作; 'http:// localhost/trackstar/index.php/project'仍然有效; 'http:// localhost/trackstar/project'重定向到'localhost'(甚至没有项目根目录) – user729103

+0

改变规则的顺序,你现在可以尝试。 – anubhava

+0

嗯,同样的结果,重定向到'localhost'。 – user729103

1

尝试添加到的.htaccess RewriteEngine on继:

RewriteBase /trackstar/ 

问题在:当没有找到文件时,它重定向到http://localhost/index.php,在r eQUEST的http://localhost/trackstar/index.php/project文件index.php被发现,它工作正常

更新: 尝试修改urlManager设置(添加/trackstar/):

'urlManager' => array(
    'urlFormat' => 'path',    
    'rules' => array(
     'commentfeed' => array('comment/feed', 'urlSuffix' => '.xml', 'caseSensitive' => false), 
     '/trackstar/<controller:\w+>/<id:\d+>' => '<controller>/view', 
     '/trackstar/<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', 
     '/trackstar/<controller:\w+>/<action:\w+>' => '<controller>/<action>', 
    ), 
    'showScriptName' => false, 
), 
+0

没有变化,仍然被重定向到'index.php'。 – user729103

+0

尝试新的更新 – CreatoR

+0

同样的结果,重定向到'index.php' – user729103

0

你可以做到这一点与2个简单的步骤。

  1. 配置 'urlManager' 在配置/ main.php看起来像这样

     'urlManager'=>array(
          'urlFormat'=>'path', 
          'showScriptName'=>false,  
         ), 
    
  2. 在用下面的代码

    的应用程序的根添加.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 
    

就是这样!

+0

这就是我开始使用,但没有为我工作。 – user729103

0

改变你的config/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>', 
     ), 

然后将你的htaccess文件与文件夹的保护(可以保护内部的文件夹)的地方下面的代码基目录到你的htaccess文件

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 

或尝试用这种---

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

做...希望它会工作