Clean urls - it's possible?


I just started with yii. I’m on localhost.

Somebody please tell me it’s possible to have truly clean urls like localhost/framework/mysite/about, localhost/framework/mysite/contactus, localhost/framework/mysite/search, etc.

As usual, I tried all kind of things but nothing works. I read many incomplete topics with all kind of configurations but nothing works.

I tried many things at protected/main/config.php with urlManager but one thing leads to completely white pages, others to url/index.php/something/something

Is there a step-by step tutorial or copy/paste, out-of-box configuration pack?

Thank you

Sure. You need to do two things, both are on this page: URL Management

You also need to create a .htaccess file to get rid of the index.php portion of the generated URL. The details of that are further down the page. Be sure to browse the comments at the end of the page as well in case something additional matches your situation.

Thanks, bglee for the response.

I followed the article, tried all kind of things with httpd.conf, .htaccess and urlManager, but still isn’t working. My biggest problem is that I found everywhere FRAGMENTS of information, not the simple, clean, focused information. For example many folks write: “turn on AllowOverride”. But there is no syntax, how to do it. Also there are 3 occurrences in that really messy config file… Ahh.

So if anybody was/is in my situation, and made the things working, please help me…

Here is what I have currently:

  • I’m on win7 64 bit

  • installed mysql

  • installed apache 2.2. Configured the webroot to E:/www. (I always restart the server when changed something in conf)

  • installed php 5.3.8. Configured the time zone in php.ini, configured php in the httpd.conf

  • extract yii in e:\www. If I make an app with "yiic webapp blabla", it works

Everything is working fine until this point in my learning curve. I’m stuck at making url’s fine. I always get stuff like localhost/framework/testdrive/index.php/site/index. I want instead localhost/framework/testdrive/index or localhost/framework/testdrive/contact, etc.

It is possible to have clean urls on localhost?

If yes, please tell me:

  1. what to change to what in httpd.conf

  2. what to write in .htaccess and where to put this file (I just saw too many configurations that is supposed to do the same thing, so I’m totally confused) I can’t see how to check if the .htaccess file is actually working (doing what is supposed to do)

  • what to write in the array of urlmanager: ‘urlManager’=>array( ? );

  • is there anything else to make/change?

I’m sorry if my post sounds childish or stupid but I simply tried so many things that simply doesn’t work.

Thanks in advance.

It’s absolutely possible to make it working on localhost.

in httpd.conf look for that line

LoadModule rewrite_module modules/

and make sure there is no ‘#’ in front of it (# marks line as comment). Restart apache.

Create .htaccess in directory where your index.php is, and paste following code:

IndexIgnore */*

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

Inside your yii application config file (default is main.php in protected/config dir) look for ‘components’ key then inside look for ‘urlManager’ key. Code following is part of my config file:

'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>',



That’s exactly what I do after creating a new app and it works for me.

If you want to get rid of site/contact part of url you will propably have to edit ‘rules’ part of urlManager configuration. However it’s a huge topic and you should look for answers in manual, and CUrlManager class documentation.

I checked/made all the things following your example. (In fact I realized that I’ve done this many times but got not working links)

I made a fresh webapp, called aaa.

Here is what I get when I click a link:

[i]Not Found

The requested URL /framework/aaa/site/index was not found on this server.[/i] (This is for Home, address bar: localhost/framework/aaa/site/index

The same with all links:

About: localhost/framework/aaa/site/page?view=about - The requested URL /framework/aaa/site/page was not found on this server.

Contact: localhost/framework/aaa/site/contact - The requested URL /framework/aaa/site/contact was not found on this server.

What could be wrong?

If I comment the line

'showScriptName' => false,

in the urlManager array, everything works, but like this:

Home: localhost/framework/aaa/index.php/site/index

About: localhost/framework/aaa/index.php/site/page?view=about

Contact: localhost/framework/aaa/index.php/site/contact

I suppose I don’t need any other extra rules in the urlManager (except those given in your example), for the default webapp…

FINALLY it’s working. Just another tinty setting must be changed.

See the red lines:

[color="#006400"]/* moved to the right forum */[/color]


I following your step by step, it’s working :)

it work !

tx a lot :)

Tx, it works.