How to remove index.php from URL

Hi!

Iam newbie to YII framework and using its latest vesion 1.1.0. I have enabled seo friendly urls through config file. My next problem is that how can I remove 'index.php' from URL. Iam using following .htaccess file.



Options +FollowSymLinks

IndexIgnore */*

RewriteEngine on


# 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




Can some one guide me how to solve this.

Thanks in advance

.htaccess is ok.

Is apache rewrite engine turned on?

Also could be nice to see your config file.

Yes Module_Rewrite is enabled on my WAMP server.

Following is my config file.




<?php


// uncomment the following to define a path alias

// Yii::setPathOfAlias('local','path/to/local-folder');


// This is the main Web application configuration. Any writable

// CWebApplication properties can be configured here.

return array(

	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

	'name'=>'My Web Application',


	// preloading 'log' component

	'preload'=>array('log'),


	// autoloading model and component classes

	'import'=>array(

		'application.models.*',

		'application.components.*',

	),


	// application components

	'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>true,

		),

		// uncomment the following to enable URLs in path-format

		

		'urlManager'=>array(

			'urlFormat'=>'path',

			'rules'=>array(

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

			),

		),

		

                'db'=>array(

			'connectionString' => 'sqlite:protected/data/testdrive.db',

		),

		// uncomment the following to use a MySQL database

		/*

		'db'=>array(

			'connectionString' => 'mysql:host=localhost;dbname=testdrive',

			'emulatePrepare' => true,

			'username' => 'root',

			'password' => '',

			'charset' => 'utf8',

		),

		*/

		'errorHandler'=>array(

			// use 'site/error' action to display errors

            'errorAction'=>'site/error',

        ),

		'log'=>array(

			'class'=>'CLogRouter',

			'routes'=>array(

				array(

					'class'=>'CFileLogRoute',

					'levels'=>'error, warning',

				),

				// uncomment the following to show log messages on web pages

				/*

				array(

					'class'=>'CWebLogRoute',

				),

				*/

			),

		),

	),


	// application-level parameters that can be accessed

	// using Yii::app()->params['paramName']

	'params'=>array(

		// this is used in contact page

		'adminEmail'=>'webmaster@example.com',

	),

);





'urlFormat'=>'path',

'showScriptName'=>false,

1 Like

Thanks for guidance.

One more thing.

How can change static pages urls. E.g; following is url for About page.

http://localhost/main/yii/wf/site/page?view=about

How can I change it to more friendly url by removing ā€˜?ā€™ sign from url.

Thanks in advance

add route:


'~<view:\w+'=>'site/page'

Thanks for the tip but being a newbie itā€™s all a little fuzzy to me!

So can you let me know where to add the above view route,

I canā€™t seem to find info on it in the docs anywhere.

Check: http://www.yiiframework.com/doc/guide/topics.url

Thanks Mike.

Really helps to know where to look:)

Now all I have to do is figure out what it all means - ah the joy of learning something new :unsure:

thank very much.iā€™m yii beginer

Hi,

Iā€™ve been trying to enable Yii under https on Apache2 and everything works fine,

except that the usual .htaccess to hide index.php (as shown in the initial post)

does not seem to work.

If I move the rewrite rules to the virtual host config, it works; but I would

rather have them in the .htaccess file.

Anyone experiences this? Any suggestions?

Thanks!

This usually means, that AllowOverride is set to None for this directory (or one of its parents directories) in your Apache config, which means: Do not allow to override settings in a .htaccess file.

Thanks man for the precise observation, it works!

I tried to match the settings for both my http and https virtual hosts when setting up SSL,

and, indeed, for some reason I overlooked "AllowOverride"ā€¦ my bad.

Hi, I tried a bit with your hint and I ended up adding this line:


'site/page/<view:\w+>'=>'site/page',

in the urlManager portion in config/main, placing it before the other rules.

The result is now that the default "about" page have this url:

/site/page/about

And the rest of the site works.

My question is: Did I do that correctly? It doesnā€™t look like your code at allā€¦!!!

I know this is a year old. But wanted to say itā€™s great thereā€™s a good community here as I look around through the forum. If you had not asked the OP for his config file, the documentation or my interpretation of it had me actually trying to modify files in the framework to get this to work.After reading this I looked at the config file and saw the code already auto generated and commented out. Both the tour videos and the first couple chapters of the new yii book say it is easy to do but they donā€™t tell how to do it. Frustrating but glad thereā€™s an active community. Iā€™m really liking Yii since I started this morning. :slight_smile:

To remove ā€œindex.phpā€ from your URLs we need to tell the server to parse all files as though they did have ā€œindex.phpā€ in the URL, but just not show it to the user.

RewriteEngine on

RewriteCond $1 ^(weblog|member|search|Forum_Name|TemplateGroup_4_Name|TemplateGroup_5_Name|P[0-9]{2,8}) [NC]

RewriteRule ^(.*)$ /index.php/$1 [L]

where i must put that .htaccess file??

Put .htaccess on your root application folder. On the same level as the index.php, index-test.php.

Hi,

I enabled the url manager and the index.php part in the url gets removed and works fin

my .htacsses file




RewriteEngine on


# 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]



the url manager works fine ut index.php doesnā€™t go away in the url

so I added when I have showScriptName set to false in my config/main.php 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>',

	),



but now I get a 404 error whenever I click a link(because it removed index.php from the url)

Hi,

I have a problem with .htaccess file. I have made the following configuration changes.

  1. removed the commented lines for rewrite in httpd.conf file.

    LoadModule rewrite_module modules/mod_rewrite.so

  2. Created new .htaccess file in the root directory of the website (In the place where we have index.php file) with following content:

RewriteEngine on

if a directory or a file exists, use it directly

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

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

RewriteCond $1 !^(index.php)

otherwise forward it to index.php

RewriteRule ^(.+)$ index.php?$1 [PT,L,QSA]

(I am using windows OS).

  1. Updated the main.php in config with following info:

ā€˜urlManagerā€™=>array(

		'urlFormat'=&gt;'path',


		'rules'=&gt;array(


			'&lt;controller:&#092;w+&gt;/&lt;id:&#092;d+&gt;'=&gt;'&lt;controller&gt;/view',


			'&lt;controller:&#092;w+&gt;/&lt;action:&#092;w+&gt;/&lt;id:&#092;d+&gt;/*'=&gt;'&lt;controller&gt;/&lt;action&gt;',				


			'&lt;controller:&#092;w+&gt;/&lt;action:&#092;w+&gt;/*'=&gt;'&lt;controller&gt;/&lt;action&gt;',


		),


		'showScriptName'=&gt;false,


	),

Issue:

I am getting 404 errorā€¦

Please help me on this.

Thank you.