我想我很接近。我有 htaccess 重定向到网站(前端/网络)和/admin
路径(backend/web
)。网站看起来很好,CSS文件加载等。
如果您转到:http://localhost/yii2app/- 它会加载主页,并且不会在地址栏中重定向,但页面在所有 URL 中显示前端/网络。
如果您转到:http://localhost/yii2app/admin - 它会加载后端登录页面,但它会立即重定向到地址栏中的/后端/网站/站点/登录(丑陋)。
问题:frontend/backend
路径显示在网址(地址栏和页面上的链接)中。
我需要什么:我希望整个网站在不显示前端/后端链接的情况下运行。项目的根目录应该(不可见地)从frontend/web
中提取而不显示它。所以 http://localhost/yii2app/运行我的整个前端,http://localhost/yii2app/admin/运行我的整个后端。
为什么?我觉得这种设置在服务器上运行时会非常坚固和优雅。我希望能够将我的项目文件夹实时推送到站点,并且它工作得很好,而无需使用黑客来处理本地与服务器。
.htaccess
文件/yii2app
目录中:
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>
现在在前端和后端 Web 目录中,它们都具有相同的.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
我不想看到/frontend/web
或/backend/web
永远:)
我试图在根目录的 htaccess 中使用重写规则来向 URL 添加/admin
,但它一直告诉我/admin
不存在。我知道它不存在,我不希望它存在。我希望它是一个相对路径。即:/admin ==/backend/web。
换一种说法。我用项目根目录 (http://localhost/yii2app/) 中的所有内容来加载frontend/web
,但没有显示它。此外,http://localhost/yii2app/admin 加载backend/web
,只显示 http://localhost/yii2app/admin。显然,他们会附上各自的controller/action
。所以管理员可能看起来像 http://localhost/yii2app/admin/site/login
注意:我没有玩过任何文件。这是一个股票 yii2 高级设置,使用作曲家,并遵循文档。到目前为止,我唯一玩过的是提到的htaccess文件。
谢谢!
试试这个 .htaccess 方法-
步骤 1
在根文件夹中创建.htaccess文件,即advanced/.htaccess
并编写以下代码。
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
注意:如果您在本地服务器中尝试,请将^/
替换为您看到箭头符号的^/project_name/
。安装完成后<------
删除这些箭头符号。
步骤 2
现在在公共目录中创建一个components/Request.php
文件,并在此文件中编写以下代码。
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();
}
}
}
步骤 3
安装组件。分别在frontend/config/main.php
和backend/config/main.php
文件中编写以下代码。
//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,
],
步骤 4(可选,如果在第三步之前不起作用)
在网络目录中
创建 .htaccess 文件
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
注意:确保您已在 apache 中启用了 mod 重写
就是这样!您可以尝试您的项目
www.project.com/admin
, www.project.com
在本地服务器
中 localhost/project_name/admin
, localhost/project_name
如果您的唯一目标是实现永远看不到/frontend/web
或/backend/web
,即使不使用 .htaccess 规则,您也可以执行以下操作:
为什么不直接拉出web
文件夹中的内容并将它们放在根目录中?只需调整入口脚本中引用框架和配置文件的路径index.php
。
您的目录结构如下所示:
- yii2app/
- frontend/
- backend/
- common/
- .. other folders..
- admin/
- assets/
- css/
- index.php
- assets/
- css/
- index.php
然后,您的yii2app/index.php
将如下所示:
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/common/config/bootstrap.php');
require(__DIR__ . '/frontend/config/bootstrap.php');
$config = yii'helpers'ArrayHelper::merge(
require(__DIR__ . '/common/config/main.php'),
require(__DIR__ . '/common/config/main-local.php'),
require(__DIR__ . '/frontend/config/main.php'),
require(__DIR__ . '/frontend/config/main-local.php')
);
$application = new yii'web'Application($config);
$application->run();
您的yii2app/admin/index.php
如下所示:
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../common/config/bootstrap.php');
require(__DIR__ . '/../backend/config/bootstrap.php');
$config = yii'helpers'ArrayHelper::merge(
require(__DIR__ . '/../common/config/main.php'),
require(__DIR__ . '/../common/config/main-local.php'),
require(__DIR__ . '/../backend/config/main.php'),
require(__DIR__ . '/../backend/config/main-local.php')
);
$application = new yii'web'Application($config);
$application->run();
编辑:您的入口脚本可能看起来与我的不同,但您应该通过这些示例更改路径以查找框架文件。
就像@deacs说的那样,只需将文件从前端/Web移动到yii2app(根文件夹),然后在yii2app"admin"中创建一个文件夹,然后将文件从后端/Web移动到yii2app/admin,然后在admin和yii2app中创建.htaccess文件使用以下代码:
Options +FollowSymLinks
IndexIgnore */*
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
然后在配置文件 main 中添加/修改 urlManager 组件.php在前端/config/main 和后端/config/main.php.php使用以下代码:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
],
],
然后使用以下代码在 yii2app 中更改索引.php:
<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/common/config/bootstrap.php');
require(__DIR__ . '/frontend/config/bootstrap.php');
$config = yii'helpers'ArrayHelper::merge(
require(__DIR__ . '/common/config/main.php'),
require(__DIR__ . '/common/config/main-local.php'),
require(__DIR__ . '/frontend/config/main.php'),
require(__DIR__ . '/frontend/config/main-local.php')
);
$application = new yii'web'Application($config);
$application->run();
还要使用以下代码更改索引.php yii2app/admin 中的索引:
<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../common/config/bootstrap.php');
require(__DIR__ . '/../backend/config/bootstrap.php');
$config = yii'helpers'ArrayHelper::merge(
require(__DIR__ . '/../common/config/main.php'),
require(__DIR__ . '/../common/config/main-local.php'),
require(__DIR__ . '/../backend/config/main.php'),
require(__DIR__ . '/../backend/config/main-local.php')
);
$application = new yii'web'Application($config);
$application->run();
这就是您在 yii2 中完成 SEO 友好 URL 所需的全部内容。我自己在挣扎,然后我从@deacs答案中得到了帮助,我想这也许会对某人有所帮助。
经过 2 天寻找最佳解决方案,我做到了(在共享主机上托管)。我制作子域admin.xxxxx.com
并将其指向文档根/public_html/xxxxxx/backend/web
由于共享主机不允许您为主域放置自定义文档根目录,因此我使用此解决方案:
更改您帐户的主域名,然后将旧的主域名添加为附加域名。这样,您可以为主域/新插件域选择所需的根文件夹。(新的主域名现在将指向public_html。
然后将(现在是我的插件域)指向前端的右文档根目录 /public_html/xxxxxx/frontend/web
我按照"执事"的答案得到了以下错误
Invalid Configuration – yii'base'InvalidConfigException
The directory does not exist: D:/wamp/www/yii2app/assets
然后我在"Yii2App"中创建了"资产"文件夹,它可以工作
----------第二种方法------------------------
在不移动文件的情况下,您可以点击以下链接
https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/start-installation.md
----------第三种方法-----------------------------
http://www.yiiframework.com/doc-2.0/guide-tutorial-shared-hosting.html
到目前为止,对我来说最简单,最灵活的解决方案是符号链接。您可以将项目放在任何地方,并且只创建指向托管强制目录的符号链接。例如,将项目放在~/domains/example.com/project
中并创建指向public_html
目录的符号链接。
cd ~/domains/example.com
# remove old public_html directory
mv public_html old_public_html
# create symlink for fronted
ln -s ./project/frontend/web public_html
# create symlink for backend in /admin subdirectory
ln -s ./project/backend/web public_html/admin
在那里,您可以使用前端http://example.com/
,http://example.com/admin/
与后端一起使用。
如果您需要在单独的域中后端(admin.example.com
):
ln -s ~/domains/example.com/project/backend/web ~/domains/admin.example.com/public_html
优点
- 没有几乎没有人理解的扭曲重写规则。
- 您在公共目录中没有整个项目,因此Web服务器和/或PHP的一些错误配置将不会公开您的代码和配置文件。
- 您可以将项目调整为托管所需的任何结构。
- 如果您使用相对符号链接,则可以将其置于版本控制之下,并使用所有应用程序作为子目录创建单个 webroot。
缺点
- 如果您的主机具有一些受限制的
open_basedir
设置而无法对其进行任何控制,则可能无法使用它。
将 .htaccess 放入 Web 目录
法典
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