我觉得我很亲近。我有重定向到网站(前端/web)和/admin路径(backend/web)的htaccess。网站显示良好,CSS文件加载等。
如果您转到:http://localhost/yii2app/ -它会加载主页,不会在地址栏中重定向,但是页面会在所有URL中显示前端/web。
如果您转到:http://localhost/yii2app/admin -它加载后端登录页面,但是它会立即重定向到地址栏中的/ backend /web/site/login (丑陋)。
问题:frontend/backend路径显示在URL中(地址栏和页面上的链接)。
我需要什么:我希望整个网站的运作,而不显示前端/后端链接。项目的根应该在没有显示的情况下(无形地)从frontend/web中拔出。因此,http://localhost/yii2app/运行我的整个前端,http://localhost/yii2app/admin/运行我的整个后端。
为什么?我觉得在服务器上运行时,这个设置将是非常可靠和优雅的。我希望能够将我的项目文件夹实时推送到一个站点,并且它可以正常工作,而不必使用黑客来处理本地和服务器。
.htaccess文件在/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>现在,在前端和后端的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中使用RewriteRule将/admin添加到URL,但它一直告诉我/admin不存在。我知道它不存在,我也不希望它存在。我希望这是一条相对的道路..。ie: /admin == /后端/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高级设置,使用composer,并遵循文档。到目前为止,我所玩的唯一一件事就是提到的htaccess文件。
谢谢!
发布于 2015-01-23 21:54:37
如果您的唯一目标是实现从未见过/frontend/web或/backend/web,即使不使用.htaccess规则,则可以进行以下操作:
为什么不直接取出web文件夹的内容并将它们放在根中呢?只需调整条目脚本index.php中引用框架和配置文件的路径即可。
您的dir结构看起来应该是:
- 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();编辑:在我看来,您的条目脚本看起来可能不一样,但是您应该使用这些示例更改路径以找到框架文件。
发布于 2015-05-02 15:09:45
用.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(可选,如果要到第三步才能工作)
在web目录中创建.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
发布于 2016-03-23 09:55:48
就像@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然后在前端/ config / main.php和后端/config/main.php的配置文件main.php中添加/修改main.php组件,代码如下:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
],
],然后用以下代码更改index.php中的yii2app:
<?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:
<?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。我挣扎着自己,然后我得到了@deacs回答的帮助,我想也许这会对某人有所帮助。
https://stackoverflow.com/questions/28118691
复制相似问题