Yii 2.0 - Basci App - I Miss The .htaccess File.

Hi,

I am waiting for the Yii2.0. And I downloaded the basic yii 2.0 app from github. I like the directory structure. But I have a problem: I miss the .htaccess file.

I can’t hide the subfolder in my redirected URL by the .htaccess

I saw some solutions here, but they didn’t help me. My index.php is here: http://localhost/basic-2/web/index.php.

And I would like to aim that If I type the http://localhost/basic-2, the browser redirect me to the http://localhost/basic-2/web/index.php path, but in the browser the URL be the http://localhost/basic-2/index.php address. So I would like to remove (or hide) the web subfolder from the URL.

I have seen many solutions here… but nothing :(, Now I have this:


Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/web/$1 -f [NC]

RewriteRule ^([^/]+)$ /web/$1 [P,L,QSA]

But this is only list the folders and files… and not make redirect to the subfolder.

This can redirect, but doesn’t hide the web folder from the URL.


RewriteEngine On

RewriteRule ^$ web [L]

Edit1 I found another rules:


RewriteEngine On

RewriteRule ^$ web/

RewriteCond %{REQUEST_FILENAME} !-f 

RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.*)$ web/$1

This can redirect! But this rule doesn’t hide(or remove) the web folder still…

Anybody or the staff have got any solution? :)

I like the Yii2.0, but I can’t wait too much. :)

.htaccess for 2.0 should be the same as for 1.1.

Yes I tired with it (1.1.). But it doesn’t work for me. Okay, I continue find other solutions.

I would also like to know how to get rid of the "web in a url"

current:

http://localhost/basic/web/about.html

I would like:

http://localhost/basic/about.html

config:





        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

            'suffix' => '.html',

            'rules' => [

                '' => 'site/index',

                '<action>'=>'site/<action>',

            ],



web/.htaccess:




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



What do I need to do?

You should create VirtualHost in Apache and set DocumentRoot dir to your "…/basic/web" dir (full system path).

Also you will need to add virtual local domain to your system "hosts" file (domain should be linked to 127.0.0.1 IP).

you will not be able to get rid of basic/web/ while you use general http://localhost/ domain name

Isn’t it just a setting in the config that prevents the urls being prefixed with that folder name?

No, there is a setting ‘showScriptName’ => false, that will hide “index.php” from url

but you cant hide those part with subfolders while you use "localhost"

For Basic Applications:




Options +FollowSymLinks

IndexIgnore */*

RewriteEngine on


# if request dosn't start with web add it

RewriteCond %{REQUEST_URI} !^/(web)

RewriteRule (.*) /web/$1


# if file or directory dosn't exists go to /web/index.php 

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /web/index.php 



For Advanced Applications:




Options +FollowSymLinks

IndexIgnore */*

RewriteEngine on





# if request begins with /admin remove admin and ad /backend/web/

RewriteCond %{REQUEST_URI} ^/admin

RewriteRule ^admin\/?(.*) /backend/web/$1


# other requests add /frontend/web/$1

RewriteCond %{REQUEST_URI} !^/(frontend/web|backend/web|admin)

RewriteRule (.*) /frontend/web/$1


# if frontend request 

RewriteCond %{REQUEST_URI} ^/frontend/web

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /frontend/web/index.php 


# if backend request

RewriteCond %{REQUEST_URI} ^/backend/web

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /backend/web/index.php 




Don’t forget that the .htaccess file needs to go in the web folder, not in the project root (or whatever you rename web to)

please, can you copy/paste the right .htaccess of provide us a link where we can find it?

I’ve the same problem and actually I’m stopped at the stage where my virtual host domain (web.app.it) open me the folders/files dir and do not execute the index.php …

This is minimum that should work.




RewriteEngine on

# 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



I tried this




<VirtualHost *:80>

   ServerName web.app.it

   DocumentRoot "C:\xampp\htdocs\advanced\frontend\web"

</VirtualHost>



and this into advanced\common\config\main.php




<?php

return [

    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',

    'components' => [

        'cache' => [

            'class' => 'yii\caching\FileCache',

        ],

        'urlManager' => [

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

            'enablePrettyUrl' => true,

            'showScriptName' => false,

        ],

    ],

];




Then I added an .htaccess to frontend\web




RewriteEngine on

 

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php



AND NOW WORKS !!!

Thanks

You can try Kartik-v template, he publish both basic and advanced template, can resolve your issue, thx.

Kartik-v practical basic template

I’ll not do it.

Before: Learn to manually do the thing

Then: Choose a faster way

Is there a solution for nginx?

nginx should work out of the box w/o any extra files. For config check official guide, it’s there.

Tested, it is working fine.

Thank you a lot!!!

I had 404 on all over the pages, only index.php worked.

I created the .htaccess in /sitename/web directory with the text below and it helped me. THX GUYS!

RewriteEngine on

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