Yii2.0 Url Manager Problem With The Index.php And Debugger Tool.

Hi,

How can I remove the index.php from the url?

I use this settings:




'urlManager' => [

                    'class' => 'yii\web\UrlManager',

                    'enablePrettyUrl' => true,

                    'showScriptName' => false,

                ],



And for me the index.php is very terrible in the url:


http://localhost/basic/web/index.php/betting

Yeap I setted up the .htaccess file :).

Because in the main layout when I would like to add other controllers’ url in the menu, I have to always write the index.php too:




echo Nav::widget([

				'options' => ['class' => 'navbar-nav navbar-left'],

				'items' => [

					['label' => 'Kínálat', 'url' => ['index.php/betting/index']],

				],

			]);



If I don’t do, the navigation will be fail.

And my other main problem with the URL manager is that I can’t use anymore the debugger tool, if I active the url manager.

I got this:

Sorry, not clear for me.

Is this URL http://localhost/basic/web/index.php/betting because of some bug, or it’s because you have to use ‘‘index.php/betting/index’’?

Sorry, not clear for me too :). I start again :). (And thanks for your answer in the other topic ‘DefaultRoute’)

1.

So in the basic (without the URL manager) my URL looks like:


http://localhost/basic/web/index.php?r=betting/index

After I activete the URL manager in the config the URL looks like:


http://localhost/basic/web/index.php/betting/index

For me this is not enough, I wanna somehow remove the index.php from the url, to get this more SEO friendly format:


http://localhost/basic/web/betting/index

Is this possible in Yii 2.0? (Remove somehowm the index.php from the URL, or hide it, anything solution is good for me :))

2.

If I Active the URL manager, then the Debug toolbar won’t work! This is a bug!. I wanna use it, I am from Symfony2 and the debug toolbar is a great feature! :). I need it!. I love it, I am fun of the debug toolbar :).

This is the error message (when I activate the URL manager in the config):




Not Found

The requested URL /basic/web/debug/default/toolbar was not found on this server.



3.

I think this is a bug again.

When my URL looks like in basic format (without the URL manager)


http://localhost/basic/web/index.php?r=/betting/index

In the navbar:


echo Nav::widget([

                'options' => ['class' => 'navbar-nav navbar-left'],

                'items' => [

                    ['label' => 'kínálat', 'url' => ['/betting/index']],

                ],

            ]);

The item’s url in the


'url' => ['/betting/index']]

format, which is good. It is like in the Yii 1.x. This is okay.

Now I activate again the URL manager:

URL again in this format:


http://localhost/basic/web/index.php/betting/index

But the routing won’t work with this url settings in the navbar:




'items' => [

                    ['label' => 'kínálat', 'url' => ['/betting/index']],

                ],



Then I will get this error message:




Not Found

The requested URL /basic/web/betting/index was not found on this server.



So for me this is a BUG, because in the Yii 1.x this worked fine with the URL manager too…

So the solution is


 'url' => ['index.php/betting/index']]

that I add the index.php to the url attribuate in the array of the navbar.




'items' => [

                    ['label' => 'Tippmix kínálat', 'url' => ['index.php/betting/index']],

                ],



And this works fine. :).

So I think this is a bug, I think the URL manager should works fine without the developer has to modifiy the url attribute in the navbar’s array in the layout.

Or, (this will be the best option for me) figure out how the URL manager can works without the index.php (in the url):

Use this


http://localhost/basic/web/betting/index

instead of the original format:


http://localhost/basic/web/index.php/betting/index

, the index.php is not too much seo friendly for me in the url.

Are you’re using the last version?

Also check the configs, maybe you have some extra config overriding main config settings (like web.php + local.php).

Double check your .htaccess file, mine (../web/.htaccess) :




RewriteEngine on


RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php



This is not a bug, debug toolbar works great.

I think its because 1st problem, but you can also check for custom “rules” in “urlManager”, if you’re using custom rules be sure to add rule for modules:


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

Fix the 1’st problem and you will not have this problem. This is not a bug.

Hello everybody,

I also have a similar problem as you refered.

*In web config:

  • urlManager:

    ‘urlManager’ => [

          'class' =&gt; 'yii&#092;web&#092;UrlManager',
    
    
          'baseUrl'=&gt;'',            
    
    
          'enablePrettyUrl' =&gt; true,
    
    
          'showScriptName' =&gt; false,
    
    
          'suffix' =&gt; '',
    
    
          'enableStrictParsing' =&gt; false,
    
    
          'rules' =&gt; []
    
    
        ]
    
  • ‘request’:

    ‘request’=>[

         'baseUrl'=&gt;''
    
    
         ]
    

*In .htaccess (/web/.htaccess):

RewriteEngine on

RewriteCond %{HTTP_HOST} ^mydomain.com$

RewriteCond %{REQUEST_URI} !^/

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /$1

RewriteCond %{HTTP_HOST} ^mydomain.com$

RewriteRule ^(/)?$ index.php

==>>>>>>>>>>>>>>>>>>>

From above configuration, i had more friendly urls with hidding index.php and moving root directory to /web directory.

My url become mydomain.com/ ,but a problem happens with NavBar::begin and Nav::widget.

  • In NavBar::begin : ‘brandUrl’ => Yii::$app->homeUrl return properly as ‘mydomain.com/’, (not mydomain.com/web).

  • In Nav::widget : urls in items array return properly such as ‘mydomain.com/site/about’, but as I click to about page, there is a error : “Not Found . The requested URL /web/site/about was not found on this server.”.

So, I think the problem is url in item of items array inside Nav::widget, it does not return properly.It show ‘mydomain.com/site/about’ but the error is /web/site/about not found.

My problem is so.Everybody discuss more,please.I’m sorry because my English is not well.

Thanks.

It looks like the rewrite_module is not activated in your apache config. you will need to activate it.

I had the exact same problem, I’m using apache 2.4 installed under Ubuntu 13.10, so i solved it in this way:

in ‘/var/log/apache2/error.log’ i had this error message : “/home/salem/public_html/alpha2/web/.htaccess: Invalid command ‘RewriteEngine’, perhaps misspelled or defined by a module not included in the server configuration”

(same location in Mac/iOS too, so just drop this line in a command line if using any unix os to check if u have the same issue : $ sudo gedit /var/log/apache2/error.log)

if you do, then, if like i do, you have your webserver’s DocumentRoot and your webapp in separate folders, you will need to start by setting the RewriteBase path in your htaccess file:


RewriteEngine on


RewriteBase /~salem/alpha2/web


# If a directory or a file exists, use the request directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward the request to index.php

RewriteRule . index.php

then to enable and load the mod_rewrite :




$ cat /etc/apache2/mods-available/rewrite.load

$ sudo a2enmod rewrite

$ ls -al /etc/apache2/mods-enabled/rewrite.load



Seb said once :

so i replaced all the "AllowOverride None" by "AllowOverride All" in the apache2 config file:


$ sudo gedit /etc/apache2/apache2.conf

Finally, restating the apache server:


$ sudo service apache2 restart

every thing was working great, my debugger bar works again & my urls looks like ‘/localhost/~salem/alpha2/web/category/view?id=1’

Maybe this help you:

I got the same problem and I found a simple solution:

Copy your .htaccess file under every web directory (not into the main site folder)

My setup:

  • [size="2"]Using Yii2 advanced template.[/size]
  • [size="2"]DocumentRoot: [/size][size="2"]/home/me/Sites/[/size]
  • [size="2"]App files are located in /home/me/Sites/mynewapp[/size]

I added to common/config/main-local.php




...

'urlManager' =>[

            'enablePrettyUrl' => true,

            'showScriptName' => false,

 	],

...



.htaccess file:





RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php



then I copied the .htaccess file to:

  • [size="2"]/home/me/Sites/mynewapp/backend/web/[/size].htaccess
  • [size="2"]/home/me/Sites/mynewapp/frontend/web/[/size].htaccess

…and removed it from root path:

  • [size="2"]/home/me/Sites/mynewapp/[/size]

Now everything works fine…

http://localhost/myn...backend/web/gii renders ok!.

Editing the apache2.conf file and changing the AllowOverride from None to All worked for me. Running Apache 2.4.7.

Thank you

If you are using localhost on windows for development. You have to make sure .htaccess file in your web folder (the folder contain index.php - bootstrap file) with file content as below:




RewriteEngine on


RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php



Note that: You cannot create dotfile like .htaccess under windows! So, you should use an editor like Sublime Text, create New File --> Paste the above content into it --> Save as .htaccsess

When you zip your project then upload to you web hosting, you have to double check .htaccess is on you web folder. Under window, durring packing time, your .htaccess is renamed to htaccess, so when you extract you zip file on hosting, you got htaccess instead of .htacces as you respect. Use ftp upload .htaccess file or manually create a .htaccess on your web folder on the host after extracting all.

I hope this post is helpful for you.

what if my server is nginx rather than apachae?

I solve it. My problem was that i have defined in 000-default.conf:




DocumentRoot /var/www/html/basic/web

<Directory "/var/www/html/basic">

  AllowOverride All

</Directory>



I assumed that my .htaccess included in /var/www/html/basic/ inherit to my web root directory but I have to create another one in /var/www/html/basic/web and it works.

Thanks a lot,