2013-10-18 60 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] 

做...希望它會工作