5
votes

I've been doing some experiments with Yii, to see if it will suit my needs. The first thing I wanted to enable was the user-friendly URLs.

What I want to achieve: to go from this URL webapproot/index.php?r=site/contact to this URL webapproot/contact.

What I've done:

  • Created an aplication using Yiic (php YiiRoot/framework/yiic.php webapp testdrive)
  • Followed the steps 2 and 6 of this link from Yii's documentation ("User-friendly URLs" and "Hiding index.php").

What happens is that I keep getting a 404. Any ideas of what I did wrong?

Below are some relevant excerpts to this question:

[project root]/protected/config/main.php

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

[project root]/.htaccess

RewriteEngine on

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

RewriteRule . index.php

/etc/apache2/httpd.conf

(...)
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
(...)
<Directory [path to my web projects folder]>
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
</Directory>
(...)
3
pentium's answer should solve it, unless you have a system where your webserver document root and the webapp's root are in different folders, which could lead to another problembool.dev
Hm, got it. Yeah, the /site/ problem would be solved with that, but I'd kindly ask for you to take a look at .htaccess thing too, 'cause I'm not able to access my pages now with just webapproot/index.php/site/contact (Which is the link that the CMenu generates), I need to manually add "index.php" to the URL, like this: webapproot/index.php/site/contactFalassion
your comment has me confused, do you want to remove index.php or not? with your current .htaccess (and showScriptName false) the app doesn't need index.php in the url.bool.dev
Yes, I want to remove index.php. I thought that I wouldn't need it but, when I try to access the URL webapproot/site/login, I get a "404 Not Found - The requested URL webapproot/site/login was not found on this server."Falassion
ok, this is a problem with your htaccess, check my answer in a bitbool.dev

3 Answers

3
votes

From your latest comment it looks like that you need to set RewriteBase as well. As i mentioned in my first comment to your question, if you have your webserver's DocumentRoot and your webapp in separate folders, then this problem could arise. Try this:

RewriteEngine on

RewriteBase /~username/yourwebappfolder
#rest of your rewrite conditions/rules

To debug htaccess or other server configurations, check your server error_log, for mac lion it is in the file: /var/log/apache2/error_log

3
votes

Your link must be webapproot/site/contact as in controller/action if you want just /contact you need to create a contact controller with index view.

2
votes

If you don't want to move that outside SiteController (though @Pentium10's answer is good enough), you can add a rule per page you want to extract from the /site/ path. However, this is a little dirty, because you need to try not to collide with other Controllers named equal to contact, login or whatever:

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

You are basically telling Yii that any request to /contact is actually a request to /site/contact. If you are at the beginning of the project creation and you are still with the basic default layouts, look at the CMenu's Contact link, you will see that now it points to webapproot/contact instead of webapproot/site/contact

Have a nice day