Yii Framework Forum: How to remove index.php from URL - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

How to remove index.php from URL Rate Topic: ***** 2 Votes

#1 User is offline   Blue Sapphire 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 27-February 10

Posted 11 March 2010 - 09:50 AM

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
2

#2 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 11 March 2010 - 10:07 AM

.htaccess is ok.

Is apache rewrite engine turned on?

Also could be nice to see your config file.
1

#3 User is offline   Blue Sapphire 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 27-February 10

Posted 11 March 2010 - 10:34 AM

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


1

#4 User is offline   jerry2801 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 18-November 09

Posted 11 March 2010 - 10:38 AM

'urlFormat'=>'path',
'showScriptName'=>false,

2

#5 User is offline   Blue Sapphire 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 27-February 10

Posted 11 March 2010 - 10:51 AM

Thanks for guidance.

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

http://localhost/mai...page?view=about

How can I change it to more friendly url by removing '?' sign from url.

Thanks in advance
1

#6 User is offline   jerry2801 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 18-November 09

Posted 11 March 2010 - 11:00 AM

View PostBlue Sapphire, on 11 March 2010 - 10:51 AM, said:

Thanks for guidance.

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

http://localhost/mai...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'

3

#7 User is offline   skeeta 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 22-June 10

Posted 22 June 2010 - 08:27 PM

View Postjerry2801, on 11 March 2010 - 11:00 AM, said:

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.
0

#8 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 23 June 2010 - 01:50 AM

Check: http://www.yiiframew...uide/topics.url
0

#9 User is offline   skeeta 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 22-June 10

Posted 23 June 2010 - 02:49 AM

View PostMike, on 23 June 2010 - 01:50 AM, said:


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:
0

#10 User is offline   Phuong Nguyen 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 31-October 10

Posted 31 October 2010 - 11:10 AM

View Postjerry2801, on 11 March 2010 - 10:38 AM, said:

'urlFormat'=>'path',
'showScriptName'=>false,


thank very much.i'm yii beginer
0

#11 User is offline   Brasuca 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 05-August 10
  • Location:Belo Horizonte, Brazil

Posted 06 December 2010 - 03:18 PM

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

#12 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 06 December 2010 - 03:21 PM

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.
0

#13 User is offline   Brasuca 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 05-August 10
  • Location:Belo Horizonte, Brazil

Posted 06 December 2010 - 03:29 PM

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.
0

#14 User is offline   luca.udinesi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 02-August 11

Posted 02 August 2011 - 10:12 AM

View Postjerry2801, on 11 March 2010 - 11:00 AM, said:

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



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...!!!
1

#15 User is offline   iansane 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 04-November 11

Posted 04 November 2011 - 07:39 PM

View Postandy_s, on 11 March 2010 - 10:07 AM, said:

.htaccess is ok.

Is apache rewrite engine turned on?

Also could be nice to see your config file.


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. :-)
0

#16 User is offline   creater 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 05-November 11
  • Location:Canada

Posted 05 November 2011 - 07:17 AM

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]
White pixels is a seo web design company which provides cheap seo services..
0

#17 User is offline   angela88 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 144
  • Joined: 14-October 11
  • Location:Jakarta

Posted 17 November 2011 - 03:54 AM

where i must put that .htaccess file??
0

#18 User is offline   Daniel 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 214
  • Joined: 26-September 09

Posted 18 November 2011 - 06:21 PM

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

#19 User is offline   Draz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 23-March 12

Posted 23 March 2012 - 05:13 PM

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)
0

#20 User is offline   HBK 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 06-December 11
  • Location:Bangalore

Posted 22 April 2012 - 01:13 PM

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).

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

'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>',
),
'showScriptName'=>false,
),

Issue:

I am getting 404 error..

Please help me on this.

Thank you.
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users