我在遵循 yii2 hard way 模板时遇到了同样的问题,然后我在 config、vhost 和 .htaccess 文件中进行了以下更改。
/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
您需要在应用程序根目录中的 .htaccess 文件中添加下面提到的代码。如果您没有,则需要在高级应用程序的根目录中创建 .htaccess 文件。
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>
虚拟主机文件
<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>