user729103
user729103

Reputation: 651

Yii - remove index.php from url - always redirected to 'index.php'

In my test application, I'm trying to remove index.php from the url. I can access the root page like so http://localhost/trackstar/ instead of http://localhost/trackstar/index.php, but as soon as I'm trying to access another page like http://localhost/trackstar/project or any other page I'm always ending up at the index.php page. Opening http://localhost/trackstar/index.php/project works however.

I'm using WAMP server. Configuration as below.

Rewrite module in apache is enabled. Config 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 in root folder of project:

Options +FollowSymlinks
IndexIgnore */*
RewriteEngine On

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

I've also added in httpd.conf, although not really necessary I think:

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

I've tried several combinations of extra parameters in .htaccess, but I keep getting redirected to my index.php page.

Any thoughts as to why this doesn't work?

Edit:

Since I'm being redirected, the checks in RewriteCond don't succeed, in other words the REQUEST_FILENAME isn't found as a directory or file. Maybe there's something off in my directory structure?

enter image description here

The TrackStar\protected\views\project directory, for example is the page I'm trying to access via http://localhost/trackstar/project. My Yii framework files are in a directory above the www folder.

Upvotes: 0

Views: 3433

Answers (4)

Ankit Rathi
Ankit Rathi

Reputation: 11

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

Upvotes: 0

CreatoR
CreatoR

Reputation: 1652

Try to add into .htaccess following after RewriteEngine on:

RewriteBase /trackstar/

The problem in: when not found file it redirects to http://localhost/index.php, in request http://localhost/trackstar/index.php/project file index.php is found and it works correctly

UPDATED: Try modify your urlManager settings (add /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,
),

Upvotes: 1

anubhava
anubhava

Reputation: 786349

Can you try:

.htaccess in root folder of project:

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]

Upvotes: 0

leoshtika
leoshtika

Reputation: 2188

You can do that with 2 simple steps.

  1. Configure the 'urlManager' at config/main.php to look like this

            'urlManager'=>array(
                'urlFormat'=>'path',
                'showScriptName'=>false,      
            ),
    
  2. Add an .htaccess file at the root of the application with the following code

    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
    

Thats it!

Upvotes: 0

Related Questions