Hamed
Hamed

Reputation: 313

Yii2 | Redirect to backend/frontend with htaccess

I am using Yii2 advanced template on localhost and I want to redirect the user to backend/web when the url is localhost/site/admin and redirect to frontend/web when the url is localhost/site using .htaccess file.

I enabled apache rewrite and I have tried these solutions
advance template Yii2 - redirect to frontend/web using htaccess
Yii2 .htaccess redirect to backend part
Redirecting to backend via htaccess Yii2

but in all cases I get 404 Error when the url is localhost/site/admin

Not Found

The requested URL /site/admin was not found on this server.

and when the url is localhost/site the content of the root directory of my site is displayed to me.

Now my .htaccess in the root of project is :

# prevent directory listings
    Options -Indexes
    IndexIgnore */*

    # follow symbolic links
    Options FollowSymlinks
    RewriteEngine on
    RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]
    RewriteRule ^(.+)?$ frontend/web/$1

and in backend/web and frontend/web is:

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

Upvotes: 1

Views: 1487

Answers (2)

Yurii Martynchuk
Yurii Martynchuk

Reputation: 1

use it without site

localhost/admin

Upvotes: 0

Hamed
Hamed

Reputation: 313

Although I had enabled mod_rewrite through the terminal via the following command:

sudo a2enmod rewrite

But it was not enabled and I manually activated it from ‍/etc/apache2/apache2.conf.

Upvotes: 0

Related Questions