13

enter image description hereHey guyz i am a newbie in Yii framework. I want to remove the index.php from my urls. Following the yii documentation when i put the rewrite engine code in my .htaccess file and setting showScriptName to false in my config/main.php file i get the 500 internal server error. My .htaccess file is located in root folder of my application. Tell me where i am doing wrong

UPDATE:

This is the code in my .htaccess file:

# 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
4
  • 2
    Well, dont you think showing you htacess would help rather than an image.... Commented Mar 9, 2012 at 11:59
  • 1
    I'm sure the YII documentation covers this as well. Commented Mar 9, 2012 at 11:59
  • 2
    Make sure you have mod_rewrite Commented Mar 9, 2012 at 12:01
  • i have updated my post with .htaccess file code Commented Mar 9, 2012 at 12:05

9 Answers 9

15

Try this

A good description is available here

http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x

Sign up to request clarification or add additional context in comments.

2 Comments

this link is no longer working. It's better to copy paste and give propper credits to the author because links can disapear with time.
a correct link would be directly to yii documentation yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x
6

If you also want to remove /index from the main page URL, add ''=>'site/index' to the top of your urlManager rules, like so:

'urlManager'=>array(
    'urlFormat'=>'path',
    'showScriptName' => false,
    'rules'=>array(
        ''=>'site/index',
        '<action>'=>'site/<action>',

Comments

3

taken directly from yii documentation

Hiding index.php There is one more thing that we can do to further clean our URLs, i.e., hiding the entry script index.php in the URL. This requires us to configure the Web server as well as the urlManager application component.

We first need to configure the Web server so that a URL without the entry script can still be handled by the entry script. For Apache HTTP server, this can be done by turning on the URL rewriting engine and specifying some rewriting rules. We can create the file /wwwroot/blog/.htaccess with the following content. Note that the same content can also be put in the Apache configuration file within the Directory element for /wwwroot/blog.

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

We then configure the showScriptName property of the urlManager component to be false.

Now if we call $this->createUrl('post/read',array('id'=>100)), we would obtain the URL /post/100. More importantly, this URL can be properly recognized by our Web application.

Hope this helps, because solved my issue as well.

Comments

3

Do these 3 steps:

  1. Enable Url re-writing on Apache.

  2. Place .htaccess on root of your project

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php
  1. In your configuration protected/config/main.php set showScriptName to false like this to your url manager components >> urlManager
'urlManager'=>array(
  'urlFormat'=>'path',
  'rules'=>array(
      '<controller:\w+>/<id:\d+>'=>'<controller>/view',
      '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
      '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
  ),
  'showScriptName'=>false,
)

Comments

1

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

Comments

0

I just came across this thread but found that in order to get it working I not only had to follow the instrucitons for adding the .htaccess file but also had to uncomment the section below from my main config file (\protected\config\main.php):

        // uncomment the following to enable URLs in path-format

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

Comments

0

I had also this problem today when I have shifted my project to linux server. I did all ways that are shown, but not helped. Then, I have copied .htaccess file to protected directory also, then it worked.

Comments

0

Make sure that if after all your changes to .htaccess & urlManager, if it still not seems to be working to anyone of you, is that you have modified your AllowOverride parameter for root directory in Apache httpd.conf file to "All" instead of "None". After modification restart apache. Because this is the parameter which let apache decide what all parameters can be overridden in .htaccess file

Comments

0

modify apache config values

AllowOverride none to AllowOverride ALL

in httpd.conf or httpd-vhosts.conf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.