AliLotfi
AliLotfi

Reputation: 430

yii2-app-advanced on single domain not work for me

I configured my yii2 advanced according to yii official wiki for using backend and frontend in a single domain on localhost. I use the hard way and i modify file

frontend/config/main.php

and

backend/config/main.php

and added

.htaccess with mod_rewrite

according to wiki. when i go to http://localhost/ all things right.but when i go to http://localhost/site/about and other link, browser shown

Object not found!

and when i want to go to http://localhost/backend/ browser redirect me to http://localhost/backend/site/login and show

Object not found!

again. i used this wiki for past yii2 projects without any problem. but now i can't use. what is my wrong? I am iranian and my english not very good. Forgive me for bad language.

Upvotes: 1

Views: 1519

Answers (2)

Poonkodi
Poonkodi

Reputation: 99

I had faced the same problem when I follow the yii2 hard way template then I made the following changes in config, vhost and .htaccess file.

/frontend/config/main.php

return  [
        'homeUrl' => '/',
        'components' => [
            'request' => [
                'baseUrl' => '',
            ],
            'urlManager' => [
                'enablePrettyUrl' => true,
                'showScriptName' => false,
            ],
        ],
    ];

/backend/config/main.php

return  [
        'homeUrl' => '/admin',
        'components' => [
            'request' => [
                'baseUrl' => '/admin',
            ],
            'urlManager' => [
                'enablePrettyUrl' => true,
                'showScriptName' => false,
            ],
        ],
    ];

.htaccess You need to add below mentioned code in .htaccess file which is there in the root of your application. If you do not have one then you need to create .htaccess file in the root of you advance application.

Options FollowSymLinks
AddDefaultCharset utf-8

<ifmodule mod_rewrite.c>
RewriteEngine On

# the main rewrite rule for the frontend application
RewriteCond %{REQUEST_URI} !^/(backend/web|admin)
RewriteRule !^frontend/web /frontend/web%{REQUEST_URI} [L]

# redirect to the page without a trailing slash 
#RewriteCond %{REQUEST_URI} ^/admin/$
#RewriteRule ^(admin)/ /$1 [L,R=301]
# the main rewrite rule for the backend application
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin(.*) /backend/web/$1 [L]

# if a directory or a file of the frontend application exists, 
# use the request directly
RewriteCond %{REQUEST_URI} ^/frontend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward the request to index.php
RewriteRule . /frontend/web/index.php [L]

# if a directory or a file of the backend application exists, 
# use the request directly
RewriteCond %{REQUEST_URI} ^/backend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward the request to index.php
RewriteRule . /backend/web/index.php [L]

RewriteCond %{REQUEST_URI} \.(htaccess|htpasswd|svn|git)
RewriteRule \.(htaccess|htpasswd|svn|git) - [F]
</ifmodule>

vhost file

<VirtualHost *:80>
DocumentRoot "path of your project" //no need to mention backend/frontend
ServerName "example.com"
<Directory "path of your project">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
</Directory>

Upvotes: 3

AliLotfi
AliLotfi

Reputation: 430

my problem has been solved. according to this answer added extra .htaccess file to backend/web and frontend/web with this content:

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

Upvotes: 2

Related Questions