I am about to edit a web application which was created using Yii framework so that index.php is not displayed as part of the URL. It is installed in Ubuntu 11.04, running on Apache 2 with PHP 5.3.5.
Before I proceed, I verified the problem by accessing one of Yii’s default action, Contact, and in the URL what is written is
http://localhost/web/index.php?r=site/contact
They indeed need my help. *evil wink*
So here is how I did it:
1) Make sure that Apache’s Rewrite Module is enabled.
It can be enabled using a2enmod command:
Then restart Apache like:
Review your phpinfo, and search for module rewrite if it is included:
2) Set Apache’s ‘AllowOverride’ to ‘ALL’
In my case, the client is using virtualhost, so that’s the file I needed to edit. The part I need to edit looks like this:
#this is the part that needs changing. I changed 'None' to 'All'
AllowOverride All
Order allow,deny
allow from all
Then again, restart the web server.
3) Write the ‘.htaccess’ file in your web application directory
Using your favorite text editor, you may paste this script:
# 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
and save it as .htaccess (the dot is not a typo, it is important that it is included in the filename) right under the web application folder. The project I’m working on is web and is located in /var/www, so my .htaccess will be placed in /var/www/web/.
4) Edit main.php
Using again your favorite text editor, open the main.php under /protected/config folder. Look for the word ‘urlManager‘ (which is commented by default), and uncomment it if necessary.
The urlManager part of the file I am editing is untouched, so I uncommented it, and configured it in such a way that it now looks like this:
I will not discuss this part by part, but if you are interested to learn about Yii’s URL Manager, click here.
Okay now, I think I’m done…
I restarted Apache (I don’t think this is necessary in this part [correct me if I am wrong], but I restarted it anyway)…
I refreshed the page (which is currently at the Contact Page), and the site leads me to home screen,index (probably because the URL I have in my browser when I refreshed the page is looking at http://localhost/web/index.php?r=site/contact, and Yii is not able to process it? Sounds like a good sign!).
I again clicked on the Contact link. Guess what :p
In the URL it is now
http://localhost/web/site/contact
Mission accomplished.
Easy 60 bucks.
17 comments
Skip to comment form ↓
Web application development Hyderabad
August 9, 2011 at 6:00 pm (UTC 8) Link to this comment
Thank you for sharing information about web designing& development.
web development Hyderabad
August 19, 2011 at 6:39 pm (UTC 8) Link to this comment
It's been a pleasure reading your blog. I have bookmarked your website so that I can come back & read more in the future as well. Please do keep up the quality writing.
lovelucy
December 16, 2011 at 9:27 am (UTC 8) Link to this comment
Hi, could you help me with the following yii problem?
http://www.yiiframework.com/forum/index.php?/topic/26800-filter-not-work-if-2-cgridviews-on-one-page/page__gopid__129132#entry129132
Said Bakr
February 6, 2012 at 6:01 pm (UTC 8) Link to this comment
Hi,
I follow your valuable hint, Now I’m able to myhost.com/site/contact. Now I need another thing:
Is there any way to remove “site” from the url?
Macinville
February 7, 2012 at 12:05 pm (UTC 8) Link to this comment
@Said Bakr
Add this as the first rule of your urlManager rules:
Amr Draz
March 24, 2012 at 5:51 pm (UTC 8) Link to this comment
Hi,
I enabled the url manager and the index.php part in the url gets removed when I refrence my site’s root
ex: http://localhost/mysite
my .htacsses file
RewriteEngine on
[code]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
# otherwise forward it to index.php
RewriteRule ^(.+)$ index.php?$1 [PT,L,QSA]
[/code]
the url manager worked fine but index.php doesn’t go away in the url
ex: http://localhost/mysite/index.php/contact
so I added showScriptName set to false in my config/main.php file
[code]
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
''=>'site/',
'/'=>'/view',
'//'=>'/',
'/'=>'/',
),
),
[/code]
now the url is http://localhost/mysite/contact
but now it gives me a 404 error(because it removed index.php from the url)
what should i do
Macinville
March 27, 2012 at 6:02 pm (UTC 8) Link to this comment
@Amr,
Is Rewrite Module of Apache present in your phpinfo?
Amr Draz
March 27, 2012 at 6:42 pm (UTC 8) Link to this comment
yes
this is a snapshot from my ini file http://dl.dropbox.com/u/23310937/YiiIssue/phpini.JPG
and like I said it works for the main page http://dl.dropbox.com/u/23310937/YiiIssue/index.JPG
but it doesn’t get rewritten http://dl.dropbox.com/u/23310937/YiiIssue/contact.JPG
unless I add ‘showScritName’=>false to my config/main.php file
in which case I get this http://dl.dropbox.com/u/23310937/YiiIssue/contactNoindex.JPG
Macinville
March 27, 2012 at 8:35 pm (UTC 8) Link to this comment
I see. Is your urlManager like this:
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<action>'=>'site/<action>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
),
If not, kindly paste your urlManager again, but enclose them inside <code lang=”php”> and </code> to make it clearer to read.
Amr Draz
March 27, 2012 at 9:18 pm (UTC 8) Link to this comment
it’s currently like this, but yeah I just uncommented ‘showScritName’ when I get the 404
'urlFormat'=>'path',
//'showScriptName'=>false,
'rules'=>array(
//'<action>'=>'site/<action>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'meet/<id:\d+>/participants'=>'meet/participants/<\d+>',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
SP Hitech
April 3, 2012 at 4:25 pm (UTC 8) Link to this comment
I followed above process but it didn’t work for me.
Raw Url: http://localhost/yii/frontend/?r=Myfirst/test === works fine !
Friendly Url: http://localhost/yii/frontend/Myfirst/test == calls index page !
But after adding following lines in config/main.php it threw error like this : “Property “CWebApplication.urlManager” is read only. ”
‘urlManager’ => array(
‘urlFormat’ => ‘path’,
‘showScriptName’ => false,
‘rules’ => array(
‘/’ => ‘/view’,
‘//’ => ‘/’,
‘/’ => ‘/’,
),
)
P.S. I am a newbie to YII framework.
Regards
SP6Hitech
pei
May 3, 2012 at 9:44 pm (UTC 8) Link to this comment
For those who installed the app to domain root,
make sure you have this in .htaccess
RewriteBase /
it might save you some time
Alex
May 25, 2012 at 4:37 am (UTC 8) Link to this comment
Thank you! Saved me a ton of time. I always forget RewriteBase / when setting up Drupal and now with Yii.
Adrian
June 1, 2012 at 6:15 am (UTC 8) Link to this comment
Thank you… I had 5 days working on it. I missed the step 2.
Sahil
July 6, 2012 at 5:27 am (UTC 8) Link to this comment
Thanks Man! It worked for me!
salman
August 7, 2012 at 8:25 pm (UTC 8) Link to this comment
Thanks buddy! its really very helpful for me…
Thanks again
dfdfd
October 9, 2012 at 10:42 am (UTC 8) Link to this comment
lol someone paid you $60 for a simple url rewrite and you thought you had to restart your webserver? I dunno which of you is dumber!