«

»

May 18

Remove ‘index.php’ in Yii Web Application

I am about to edit a web application which was created using Yii framework so that index.php is not displayed as part of the URL. It is installed in Ubuntu 11.04, running on Apache 2 with PHP 5.3.5.

Before I proceed, I verified the problem by accessing one of Yii’s default action, Contact, and in the URL what is written is

http://localhost/web/index.php?r=site/contact

They indeed need my help. *evil wink*

So here is how I did it:

1) Make sure that Apache’s Rewrite Module is enabled.

It can be enabled using a2enmod command:

$ sudo a2enmod rewrite

Then restart Apache like:

$ sudo service apache2 restart

Review your phpinfo, and search for module rewrite if it is included:

2) Set Apache’s ‘AllowOverride’ to ‘ALL’

In my case, the client is using virtualhost, so that’s the file I needed to edit. The part I need to edit looks like this:

Options Indexes FollowSymLinks MultiViews
#this is the part that needs changing. I changed 'None' to 'All'
AllowOverride All
Order allow,deny
allow from all

Then again, restart the web server.

3) Write the ‘.htaccess’ file in your web application directory

Using your favorite text editor, you may paste this script:

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

and save it as .htaccess (the dot is not a typo, it is important that it is included in the filename) right under the web application folder. The project I’m working on is web and is located in /var/www, so my .htaccess will be placed in /var/www/web/.

4) Edit main.php

Using again your favorite text editor, open the main.php under /protected/config folder. Look for the word ‘urlManager‘ (which is commented by default), and uncomment it if necessary.

The urlManager part of the file I am editing is untouched, so I uncommented it, and configured it in such a way that it now looks like this:

'urlManager'=>array(
     'urlFormat'=>'path',
     'showScriptName'=>false,
     'rules'=>array(
       '<action>'=>'site/<action>',
       '<controller:\w+>/<id:\d+>' => '<controller>/view',
       '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
       '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
     ),
),

I will not discuss this part by part, but if you are interested to learn about Yii’s URL Manager, click here.

Okay now, I think I’m done…

I restarted Apache (I don’t think this is necessary in this part [correct me if I am wrong], but I restarted it anyway)…

I refreshed the page (which is currently at the Contact Page), and the site leads me to home screen,index (probably because the URL I have in my browser when I refreshed the page is looking at http://localhost/web/index.php?r=site/contact, and Yii is not able to process it? Sounds like a good sign!).

I again clicked on the Contact link. Guess what :p

In the URL it is now

http://localhost/web/site/contact

Mission accomplished.

Easy 60 bucks. ;)

17 comments

Skip to comment form

  1. Web application development Hyderabad

    Thank you for sharing information about web designing& development.

  2. web development Hyderabad

    It's been a pleasure reading your blog. I have bookmarked your website so that I can come back & read more in the future as well. Please do keep up the quality writing.

  3. Said Bakr

    Hi,
    I follow your valuable hint, Now I’m able to myhost.com/site/contact. Now I need another thing:
    Is there any way to remove “site” from the url?

  4. Macinville

    @Said Bakr

    Add this as the first rule of your urlManager rules:

    '<action>'=>'site/<action>',
  5. Amr Draz

    Hi,
    I enabled the url manager and the index.php part in the url gets removed when I refrence my site’s root
    ex: http://localhost/mysite

    my .htacsses file
    RewriteEngine on
    [code]
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php)

    # otherwise forward it to index.php
    RewriteRule ^(.+)$ index.php?$1 [PT,L,QSA]
    [/code]

    the url manager worked fine but index.php doesn’t go away in the url
    ex: http://localhost/mysite/index.php/contact

    so I added showScriptName set to false in my config/main.php file
    [code]
    'urlManager'=>array(
    'urlFormat'=>'path',
    'showScriptName'=>false,
    'rules'=>array(
    ''=>'site/',
    '/'=>'/view',
    '//'=>'/',
    '/'=>'/',
    ),
    ),
    [/code]

    now the url is http://localhost/mysite/contact

    but now it gives me a 404 error(because it removed index.php from the url)
    what should i do

  6. Macinville

    @Amr,

    Is Rewrite Module of Apache present in your phpinfo?

    1. Amr Draz

      yes
      this is a snapshot from my ini file http://dl.dropbox.com/u/23310937/YiiIssue/phpini.JPG

      and like I said it works for the main page http://dl.dropbox.com/u/23310937/YiiIssue/index.JPG

      but it doesn’t get rewritten http://dl.dropbox.com/u/23310937/YiiIssue/contact.JPG

      unless I add ‘showScritName’=>false to my config/main.php file
      in which case I get this http://dl.dropbox.com/u/23310937/YiiIssue/contactNoindex.JPG

      1. Macinville

        I see. Is your urlManager like this:

        'urlManager'=>array(
             'urlFormat'=>'path',
             'showScriptName'=>false,
             'rules'=>array(
               '<action>'=>'site/<action>',
               '<controller:\w+>/<id:\d+>' => '<controller>/view',
               '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
               '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
             ),
        ),

        If not, kindly paste your urlManager again, but enclose them inside <code lang=”php”> and </code> to make it clearer to read.

        1. Amr Draz

          it’s currently like this, but yeah I just uncommented ‘showScritName’ when I get the 404

          'urlManager'=>array(
                      'urlFormat'=>'path',
                      //'showScriptName'=>false,
                      'rules'=>array(
                          //'<action>'=>'site/<action>',
                          '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                          'meet/<id:\d+>/participants'=>'meet/participants/<\d+>',
                          '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                          '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                      ),
                  ),
  7. SP Hitech

    I followed above process but it didn’t work for me.

    Raw Url: http://localhost/yii/frontend/?r=Myfirst/test === works fine !
    Friendly Url: http://localhost/yii/frontend/Myfirst/test == calls index page !

    But after adding following lines in config/main.php it threw error like this : “Property “CWebApplication.urlManager” is read only. ”

    ‘urlManager’ => array(
    ‘urlFormat’ => ‘path’,
    ‘showScriptName’ => false,
    ‘rules’ => array(
    ‘/’ => ‘/view’,
    ‘//’ => ‘/’,
    ‘/’ => ‘/’,
    ),
    )

    P.S. I am a newbie to YII framework.

    Regards
    SP6Hitech

  8. pei

    For those who installed the app to domain root,
    make sure you have this in .htaccess

    RewriteBase /

    it might save you some time

    1. Alex

      Thank you! Saved me a ton of time. I always forget RewriteBase / when setting up Drupal and now with Yii.

  9. Adrian

    Thank you… I had 5 days working on it. I missed the step 2.

  10. Sahil

    Thanks Man! It worked for me!

  11. salman

    Thanks buddy! its really very helpful for me…
    Thanks again

  12. dfdfd

    lol someone paid you $60 for a simple url rewrite and you thought you had to restart your webserver? I dunno which of you is dumber!

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human?