0

在我的测试应用程序中,我试图index.php从 url 中删除。我可以像这样访问根页面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

我还添加了 in 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目录中。

4

4 回答 4

1

尝试在 .htaccess 之后添加以下内容RewriteEngine on

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,
),
于 2013-10-18T09:21:16.720 回答
0

change your config/main.php like below:

'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>',
        ),

then place your htaccess file on base directory with protected folder (it may be inside protected folder) place below code to your htaccess file

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

or try with this---

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

done...hope it will work

于 2014-09-11T12:37:04.950 回答
0

您可以通过 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
    

而已!

于 2013-10-18T13:44:51.467 回答
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]
于 2013-10-18T08:40:03.423 回答