How to remove index.php from URL


(Dr Dumb99) #1

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


(Arekandrei) #2

.htaccess is ok.

Is apache rewrite engine turned on?

Also could be nice to see your config file.


(Dr Dumb99) #3

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

	),

);





(Jerry2801) #4

'urlFormat'=>'path',

'showScriptName'=>false,


(Dr Dumb99) #5

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


(Jerry2801) #6

add route:


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


(Lesboys) #7

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.


(Mh) #8

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


(Lesboys) #9

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:


(Phuong Nguyen Itvn) #10

thank very much.i’m yii beginer


(Rodrigo Ferreira) #11

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!


(Mh) #12

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.


(Rodrigo Ferreira) #13

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.


(Luca Udinesi) #14

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…!!!


(Simmonian) #15

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:


(Wpswebinfo) #16

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]


(Angela Arrie) #17

where i must put that .htaccess file??


(Adinugro) #18

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


(Amr M Draz) #19

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)


(Sagar Krupahb2000) #20

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.