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:
php YiiRoot/framework/yiic.php webapp testdrive
)What happens is that I keep getting a 404. Any ideas of what I did wrong?
Below are some relevant excerpts to this question:
(...)
'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>',
),
),
(...)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
(...)
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
(...)
<Directory [path to my web projects folder]>
Options FollowSymLinks
AllowOverride All
Order deny,allow
</Directory>
(...)
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With