3

The problem

Initial problem: Hi I have a newly made Yii site where I want to remove the index.php from the URL.
Example: "/index.php/site/index" should be "/site/index"

I have been using this http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x guide but i only receive 404.

I hope that you are able to point a mistake or help me debug this problem!
Please let me know if I have left out any relevant information.

OBS: The "home" page works as intended, other pages are broken.

Status of the problem: It seems like it is an apache/ubuntu problem with mod_rewrite.so

Answer

After help from different people it all works now :D I had to install "rewrit" to get it running, i did that by writing Running a2enmod rewrit The below configuration of my system solved the problem for me, I hope this thread will help others in similar problems.

My system

Server version: Apache/2.2.22 (Ubuntu)  
Server built:   Nov  8 2012 21:37:45

Apache httpd.conf

<Directory "/var/www/MY_SITE/FOLDER_CONTAINING_YII/">
AllowOverride All
#...
</Directory>
LoadModule rewrite_module modules/mod_rewrite.so

This is the entire content of the file

Apache Error Log

File does not exist: /.../htdocs/site, referer: http://.../htdocs/index.php/site/index  

I added the dots

Restart Apache

kah@webaalborg:/etc/apache2/sites-available$ sudo service apache2 restart
 * Restarting web server apache2 
[Mon Nov 26 20:16:35 2012] [warn] module rewrite_module is already loaded, skipping
  ... waiting 
[Mon Nov 26 20:16:36 2012] [warn] module rewrite_module is already loaded, skipping
                                                                          [ OK ]

/ect/apache2/available-sites/default

...
DocumentRoot /var/www
    <Directory />
            Options FollowSymLinks
            AllowOverride None
    </Directory>
    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
    </Directory>
    <Directory /var/www/MY_SITE>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride all
            Order allow,deny
            allow from all
     </Directory>
...

Yii Directory structure:

  • framework
  • htdocs
    • assets
    • css
    • .htaccess
    • index.php
    • index-test.php
    • themes
  • protected

.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

main.php config file

'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>',   
        ),
    ),  
10
  • Can you show the output of apachectl -t -D DUMP_MODULES here? Commented Nov 25, 2012 at 11:33
  • check here stackoverflow.com/questions/9633649/… Commented Nov 26, 2012 at 11:52
  • This might be trivial, but have you remembered restarting apache? Commented Nov 26, 2012 at 12:07
  • @Suhoij I have added the "error" output of the dump. It seems that my system have an unforseen problem. :) Commented Nov 26, 2012 at 18:41
  • Yes, you are right. Server can not find mod_rewrite.so. Looks like the problem with apache installation. Try to remove this string from config file and enable the module with a2enmod rewrite. Commented Nov 26, 2012 at 18:44

4 Answers 4

5

If you are working on Linux(say ubuntu) then go to the path /etc/apache2/sites-available/ there you will find a file named as default,

    <Directory /var/www/> <--- ***root directory***
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all <---- ***change this line***
    Order allow,deny
    allow from all
    </Directory>

and i think your problem will resolve.

Thanks.

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

1 Comment

After i did what @suhoij wrote i did what you suggested and it works. Thanks for the help :)
1

Do you AllowOverride in your directory directive?

<Directory "/your/path">
AllowOverride All
#...
</Directory>

If not this would explain why your rewrite rules does not work.

3 Comments

I have not inserted any such code. What is my directory directive and where do i find it?
Sorry I was afk. This depends on your server configuration and on your destibution (only on linux) search for a httpd.conf or a vhost.conf.
I added it, but it did not change anything. Is there supposted to stand anything where you wrote the "..."?. A thing i forgot to mention is that the frontpage works. I do not get any error when I write site.com when I enter other pages e.g. site.com/login it displays 404.
0

Try to put this into .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

And also remove the line 'LoadModule rewrite_module modules/mod_rewrite.so' from httpd.conf and enable the rewrite module from command line, executing a2enmod rewrite.

Comments

0
<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride all <---- ***change this line***
    Order allow,deny  <---- ***change this line***
    allow from all
    Require all granted
</Directory>

1 Comment

Could you please edit in an explanation of why this code answers the question? Code-only answers are discouraged, because they are not as easy to learn from as code with an explanation. Without an explanation it takes considerably more time and effort to understand what was being done, or changes made to code. The explanation is important both for people attempting to learn from the answer and those evaluating the answer to see if it is valid, or worth up voting. Marking the lines to change is helpful, but it would be better if it was as a valid syntax comment.

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.