Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELY

I think I am pretty close. I have the htaccess redirecting to the website (frontend/web) and the /admin path (backend/web). The site appears fine, CSS files loading, etc.

If you go to: http://localhost/yii2app/ - it loads the homepage, and doesn't redirect in the address bar, but the page shows frontend/web in all the URLs.

if you go to: http://localhost/yii2app/admin - it loads the backend login page, however it immediately redirects to /backend/web/site/login in the address bar (ugly).

Problem: The frontend/backend paths are showing in the URLs (address bar, and links on the page).

What I need: I want the whole site to operate without showing frontend/backend links. The project's root should pull (invisibly) from the frontend/web without showing it.. So http://localhost/yii2app/ runs my whole frontend, and http://localhost/yii2app/admin/ runs my whole backend.

Why? I feel this setup would be pretty solid and elegant when live on a server. I want to be able to push my project folder live to a site and it work just fine without having to have hacks to handle local vs server.

.htaccess file in /yii2app dir:

Options -Indexes RewriteEngine on  <IfModule mod_rewrite.c>     RewriteCond %{REQUEST_URI} !^/backend/web/(assets|css)/     RewriteCond %{REQUEST_URI} admin     RewriteRule .* backend/web/index.php [L]      RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/     RewriteCond %{REQUEST_URI} !admin     RewriteRule .* frontend/web/index.php [L] </IfModule> 

Now in frontend and backend web directories, they both have the same .htaccess:

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 do not want to see /frontend/web or /backend/web ever :)

I tried to play with the RewriteRule in the root's htaccess to add /admin to the URL, but it kept telling me /admin does not exist. I KNOW it does not exist, and I don't want it to exist. I want it to be a relative path.. ie: /admin == /backend/web.

Worded another way. I way everything in the project's root (http://localhost/yii2app/) to load frontend/web, but without showing it. Also, http://localhost/yii2app/admin to load backend/web and just showing http://localhost/yii2app/admin. Obviously they would have their respective controller/action attached to them. So admin could look like http://localhost/yii2app/admin/site/login

NOTE: I have not played with any of the files. This is a stock yii2 advanced setup, using composer, and following the docs. The only thing I have played with so far are the htaccess files mentioned.

Thank you!

like image 898
Wade Avatar asked Jan 23 '15 21:01

Wade


People also ask

How do I access the backend of yii2app?

If you go to: http://localhost/yii2app/ - it loads the homepage, and doesn't redirect in the address bar, but the page shows frontend/web in all the URLs. if you go to: http://localhost/yii2app/admin - it loads the backend login page, however it immediately redirects to /backend/web/site/login in the address bar (ugly).

How to log out of frontend and backend?

What will happen is you login to frontend, then open a new tab and go to backend, you will be logged in as a member from the 'admin' table matching the same ID. User ID 3 will have you logged in as Admin ID 3 in the backend. If you click logout in either of them, you get logged out of both..

Where are sessions stored in yii2?

You can change the savePath to wherever you want, but generally sessions are stored in the system's tmp directory by default (ie: php's sessions using session_start () ). I did stumble across a page showing how to use a database for storing the sessions in Yii2. I don't like using databases for sessions.


1 Answers

Try this with .htaccess Method-

Step 1

Create .htaccess file in root folder, i.e advanced/.htaccess and write below code.

Options +FollowSymlinks RewriteEngine On  # deal with admin first RewriteCond %{REQUEST_URI} ^/(admin) <------ RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L] RewriteRule ^admin/css/(.*)$ backend/web/css/$1 [L]  RewriteCond %{REQUEST_URI} !^/backend/web/(assets|css)/  <------ RewriteCond %{REQUEST_URI} ^/(admin)  <------ RewriteRule ^.*$ backend/web/index.php [L]   RewriteCond %{REQUEST_URI} ^/(assets|css)  <------ RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L] RewriteRule ^css/(.*)$ frontend/web/css/$1 [L] RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]  RewriteRule ^images/(.*)$ frontend/web/images/$1 [L]  RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/  <------ RewriteCond %{REQUEST_URI} !index.php RewriteCond %{REQUEST_FILENAME} !-f [OR] RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^.*$ frontend/web/index.php 

Note : if you are trying in local server then replace ^/ with ^/project_name/ where you see arrow sign. Remove those arrow sign <------ after setup is done.

Step 2

Now create a components/Request.php file in common directory and write below code in this file.

namespace common\components;   class Request extends \yii\web\Request {     public $web;     public $adminUrl;      public function getBaseUrl(){         return str_replace($this->web, "", parent::getBaseUrl()) . $this->adminUrl;     }       /*         If you don't have this function, the admin site will 404 if you leave off          the trailing slash.          E.g.:          Wouldn't work:         site.com/admin          Would work:         site.com/admin/          Using this function, both will work.     */     public function resolvePathInfo(){         if($this->getUrl() === $this->adminUrl){             return "";         }else{             return parent::resolvePathInfo();         }     } } 

Step 3

Installing component. Write below code in frontend/config/main.php and backend/config/main.php files respectively.

//frontend, under components array 'request'=>[     'class' => 'common\components\Request',     'web'=> '/frontend/web' ], 'urlManager' => [         'enablePrettyUrl' => true,         'showScriptName' => false, ],  // backend, under components array 'request'=>[     'class' => 'common\components\Request',     'web'=> '/backend/web',     'adminUrl' => '/admin' ], 'urlManager' => [         'enablePrettyUrl' => true,         'showScriptName' => false, ], 

Step 4 (Optional, if doesn't work till step three)

create .htaccess file in web directory

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

Note: make sure you have enabled your mod rewrite in apache

Thats it! You can try your project with
www.project.com/admin, www.project.com

in local server
localhost/project_name/admin, localhost/project_name

like image 196
mohit Avatar answered Oct 11 '22 17:10

mohit