Bibek Jana
Bibek Jana

Reputation: 104

Change module url in yii

I have the following url http://localhost.com/projectname/index.php?r=site/cms&view=about_us I want to change this url as following http://localhost.com/projectname/about_us, using htaccess and urlmanager then what have to to do?

Upvotes: 1

Views: 206

Answers (4)

johnsneakers
johnsneakers

Reputation: 16

in yii first you should edit main/config.php like this:

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

(this step can help you hide index.php)

and second , create a .htaccess file in document ,print this:

Options +FollowSymLinks IndexIgnore / RewriteEngine on RewriteBase /

# 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

PS: this is my config,it help your url look clear:

'rules'     => array(
        '/login'                => 'user/account/login',
        '/logout'               => 'user/account/logout',
        '/registration'         => 'user/account/registration',
),

Upvotes: 0

Let me see
Let me see

Reputation: 5094

Since Priya jain has already given an answer to you about hiding the index.php from your Url. In order to change your Url to http://localhost.com/projectname/about_us you can make the following change in your config/main.php

'urlManager'=>array(
            'urlFormat'=>'path',
                        'showScriptName'=>false,
            'rules'=>array(
                              // add this line
                                '<view:\w+>'=>'site/cms',
                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            ),
        ),

If still you are unable to hide index.php then you can try this

<ifModule mod_rewrite.c>
# Turn on the engine:
RewriteEngine on
# Don't perform redirects for files and directories that exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For everything else, redirect to index.php:
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</ifModule>

Note:- Save it as your project/.htaccess and remember the showScriptName should be false in your urlManger array() in config/main.php

Upvotes: 1

Burhan &#199;etin
Burhan &#199;etin

Reputation: 676

create .htaccess file

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]

if not works try

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

dont forget to uncomment urlmanager in protected/config/main.php file

Upvotes: 0

Priya jain
Priya jain

Reputation: 703

Try This code index.php part in the url gets removed and works fine :

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]

Upvotes: 0

Related Questions