Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELY
In a standard Yii2 Advanced application, you likely have two distinct entry points: frontend/web for your public site and backend/web for your admin panel. While the codebase separates concerns cleanly, the URL structure often reveals this internal architecture. Visiting http://localhost/yii2app/frontend/web works, but it looks messy. Worse, when you visit /admin, Yii2 immediately redirects you to /backend/web/site/login.
The goal is to abstract these physical paths away from the user. You want http://yoursite.com/ to map seamlessly to frontend/web/index.php, and http://yoursite.com/admin to map to backend/web/index.php. The final URLs should never display /frontend/web or /backend/web.
The Root Rewrite Logic
The core issue lies in the .htaccess file located in your project root (e.g., /yii2app). Your current configuration attempts to route requests, but it fails to handle the specific case where a user types /admin. When a browser requests /admin, your rules likely redirect it to the backend, but the RewriteRule destination includes the full path (backend/web/index.php). Because mod_rewrite processes this as an absolute path within the document root, the address bar updates to show the full internal URL.
To fix this, you must ensure that when a request matches /admin, the rewrite rule points to index.php inside the backend folder relative to that specific directory. This forces Apache to serve the file from the backend context without leaking the path into the user's address bar.
Here is a robust pattern for your root .htaccess:
Options -Indexes
RewriteEngine On
<IfModule mod_rewrite.c>
# --- Backend Routing ---
# If the request starts with /admin, redirect to backend/web/index.php
# The trailing slash ensures we catch /admin/ as well
RewriteCond %{REQUEST_URI} ^/admin/?$ [OR]
# Check that the file doesn't actually exist in the root
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite to backend/web/index.php
# Note: We use a relative path here to prevent address bar leaks
RewriteRule ^(.*)$ backend/web/index.php [L]
# --- Frontend Routing ---
# If the request does NOT start with /admin, send it to frontend/web
# We exclude specific static assets so they load directly from their folders
RewriteCond %{REQUEST_URI} !^/frontend/web/(assets|css|js)/
# Also ensure we aren't hitting a real file or directory in the root
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ frontend/web/index.php [L]
</IfModule>
The critical detail is avoiding absolute paths like http://localhost/yii2app/backend/web/.... By using relative paths (backend/web/index.php), Apache resolves the target relative to the current directory. If the user requests /admin, Apache looks for backend/web/index.php inside the root, serves it, and keeps the URL clean (mostly).
However, there is a nuance: if you want the address bar to show exactly what the user typed (/admin) rather than the internal backend path, you need an external redirect. This involves changing the flag from [L] to [R=301,L]. But be careful: if you use an external redirect, the browser will update the address bar to the target URL unless that target is also a clean rewrite.
A better approach for the "cleanest" URLs is to keep the internal routing relative and ensure your controllers handle the paths correctly. If you strictly require the address bar to remain /admin, you must use a 301 redirect pointing to a path that your backend controller recognizes as valid without changing the URL further.
Handling Static Assets
A common pitfall in Yii2 advanced setups is breaking CSS and JS assets. The standard public/index.php logic relies on !-f and !-d checks to bypass index.php for existing files. You must replicate this logic in your root .htaccess.
In your root file, before rewriting requests to the frontend or backend, ensure you exclude static resources:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /frontend/web/index.php [L]
If you put this logic at the very top of your root rules without checking for assets first, a user requesting /frontend/web/css/style.css might get rewritten to frontend/web/index.php, resulting in a 404 or a blank page. Always check if the file exists (%{REQUEST_FILENAME}) before rewriting.
The Backend Entry Point
Once you route /admin to backend/web/index.php, you must ensure that backend controller actions return the correct canonical URLs. If your login form submits to /admin/site/login, but the success redirect goes to /backend/web/admin/dashboard, the user sees a broken URL in the bar.
You should configure your backend controllers (e.g., SiteController) to generate URLs relative to the clean path. In Yii2, use the Url::to() helper with absolute configuration or ensure your urlManager component is configured to ignore the physical directory structure when generating links.
For example, in backend/controllers/SiteController.php:
use yii\helpers\Url;
public function actionLogin()
{
// ... login logic ...
// Redirect using a path that matches the clean URL structure
return $this->redirect(['/admin', 'site', 'index']);
}
This ensures that even though the request is physically handled by backend/web, the generated links remain consistent with the /admin prefix.
Why This Matters for Production
This abstraction layer is essential for production deployments. If you deploy your code directly to a server, having frontend/web and backend/web in the public root exposes internal architecture details. A cleaner URL structure prevents search engines from indexing duplicate content if they crawl both paths (though Yii2's canonical tags usually handle this).
More importantly, it simplifies maintenance. You can deploy the entire project folder without needing complex symlinks or hacks to make the backend accessible via a clean /admin path. The server handles the routing invisibly, allowing you to focus on the application logic rather than URL mapping quirks.
If you are maintaining a legacy project or migrating from an old framework, this pattern provides a solid foundation for a modern Yii2 Advanced application. It keeps your URLs semantic and your deployment strategy predictable.