Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User-friendly URLs in Yii

Tags:

.htaccess

yii

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>
(...)
like image 339
Falassion Avatar asked Jul 04 '12 22:07

Falassion


3 Answers

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

like image 71
bool.dev Avatar answered Nov 20 '22 15:11

bool.dev


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.

like image 36
Pentium10 Avatar answered Nov 20 '22 16:11

Pentium10


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

like image 2
Sergi Juanola Avatar answered Nov 20 '22 16:11

Sergi Juanola