【问题标题】:Yii - remove index.php from url - always redirected to 'index.php'Yii - 从 url 中删除 index.php - 总是重定向到 'index.php'
【发布时间】:2013-10-18 08:36:32
【问题描述】:

在我的测试应用程序中,我试图从 url 中删除 index.php。我可以像http://localhost/trackstar/ 那样访问根页面而不是http://localhost/trackstar/index.php,但是一旦我尝试访问另一个页面,如http://localhost/trackstar/project 或任何其他页面,我总是会在index.php 页面结束。但是打开http://localhost/trackstar/index.php/project 可以。

我正在使用 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 没有作为目录或文件找到。也许我的目录结构有问题?

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

【问题讨论】:

  • 您的网址是否正确生成?还是应用程序将您重定向到 index.php?
  • @PeterM:我的浏览器显示http://localhost/trackstar/project,但我在index.php 页面上。
  • .htaccess 是否与 index.php 位于同一目录?
  • @CreatoR:是的,在www\Trackstar 中,还有另一个.htaccess 文件位于www\Trackstar\protected 中,内容为deny from all

标签: apache .htaccess yii url-rewriting


【解决方案1】:

尝试在RewriteEngine on之后添加到.htaccess中:

RewriteBase /trackstar/

问题在于:找不到文件时重定向到http://localhost/index.php,在请求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,
),

【讨论】:

  • 没有变化,仍然被重定向到index.php
  • 同样的结果,重定向到index.php
【解决方案2】:

您可以通过 2 个简单的步骤来做到这一点。

  1. 将 config/main.php 中的“urlManager”配置为如下所示

            '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
    

就是这样!

【讨论】:

    【解决方案3】:

    你可以试试:

    项目根目录下的.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]
    

    【讨论】:

    • 谢谢,但还是不行; http://localhost/trackstar/index.php/project 仍然有效; http://localhost/trackstar/project 重定向到 localhost(甚至不是项目根目录)
    • 改变了规则的顺序,你现在可以试试。
    • 嗯,同样的结果,重定向到localhost
    • 在您上次编辑后,我现在被重定向到 index.php
    • 所以像http://localhost/trackstar/index.php 这样的URI 不会变成http://localhost/trackstar/
    【解决方案4】:

    改变你的 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]
    

    完成...希望它会工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      相关资源
      最近更新 更多