Wad*_*ade 30 php apache .htaccess mod-rewrite yii2
我觉得我很亲密.我有htaccess重定向到网站(前端/ web)和/admin路径(backend/web).该网站显示正常,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 /运行我的整个后端.
为什么?我觉得这个设置在服务器上运行时会非常稳固和优雅.我希望能够将我的项目文件夹直播到一个站点,它工作得很好,而不必有黑客来处理本地vs服务器.
.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>
Run Code Online (Sandbox Code Playgroud)
现在在前端和后端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
Run Code Online (Sandbox Code Playgroud)
我不想看到/frontend/web或/backend/web曾经:)
我尝试在root的htaccess中使用RewriteRule添加/admin到URL,但它一直告诉我/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文件.
谢谢!
moh*_*hit 29
尝试使用.htaccess方法 -
在根文件夹中创建.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
Run Code Online (Sandbox Code Playgroud)
注意:如果您在本地服务器上尝试,则替换^/为^/project_name/您看到箭头符号的位置.<------设置完成后删除这些箭头标志.
现在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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
安装组件.分别在下面写入代码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,
],
Run Code Online (Sandbox Code Playgroud)
第4步(可选,如果在第3步之前不起作用)
在web目录中创建.htaccess文件
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Run Code Online (Sandbox Code Playgroud)
注意:确保在apache中启用了mod重写
而已!您可以尝试使用
www.project.com/admin,www.project.com
在本地服务器中
localhost/project_name/admin,localhost/project_name
dea*_*acs 26
如果您的唯一目标是实现永远不会看到,/frontend/web或者/backend/web即使不使用.htaccess规则,您也可以选择以下内容:
为什么不拉出web文件夹的内容并将它们放在根目录中?只需调整引用脚本中引用框架和配置文件的路径即可index.php.
您的目录结构如下:
- yii2app/
- frontend/
- backend/
- common/
- .. other folders..
- admin/
- assets/
- css/
- index.php
- assets/
- css/
- index.php
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)
你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();
Run Code Online (Sandbox Code Playgroud)
编辑:您的条目脚本可能看起来与我的不同,但您应该想到通过这些示例更改路径以查找框架文件.