php Yii2 htaccess - 如何完全隐藏前端/网络和后端/网络
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28118691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELY
提问by Wade
I think I am pretty close. I have the htaccess redirecting to the website (frontend/web) and the /admin
path (backend/web
). The site appears fine, CSS files loading, etc.
我觉得我很接近。我将 htaccess 重定向到网站(前端/网络)和/admin
路径(backend/web
)。该站点显示正常,加载 CSS 文件等。
If you go to: http://localhost/yii2app/- it loads the homepage, and doesn't redirect in the address bar, but the page shows frontend/web in all the URLs.
如果您访问:http://localhost/yii2app/- 它会加载主页,并且不会在地址栏中重定向,但该页面会在所有 URL 中显示 frontend/web。
if you go to: http://localhost/yii2app/admin- it loads the backend login page, however it immediately redirects to /backend/web/site/login in the address bar (ugly).
如果您访问:http://localhost/yii2app/admin- 它会加载后端登录页面,但它会立即重定向到地址栏中的 /backend/web/site/login(丑陋)。
Problem: The frontend/backend
paths are showing in the URLs (address bar, and links on the page).
问题:frontend/backend
路径显示在 URL(地址栏和页面上的链接)中。
What I need: I want the whole site to operate without showing frontend/backend links. The project's root should pull (invisibly) from the frontend/web
without showing it.. So http://localhost/yii2app/runs my whole frontend, and http://localhost/yii2app/admin/runs my whole backend.
我需要什么:我希望整个网站在不显示前端/后端链接的情况下运行。项目的根应该frontend/web
从不显示它的情况下(不可见地)拉出。所以http://localhost/yii2app/运行我的整个前端,而http://localhost/yii2app/admin/运行我的整个后端。
Why? I feel this setup would be pretty solid and elegant when live on a server. I want to be able to push my project folder live to a site and it work just fine without having to have hacks to handle local vs server.
为什么?我觉得这个设置在服务器上运行时会非常可靠和优雅。我希望能够将我的项目文件夹实时推送到一个站点,并且它可以正常工作而无需进行 hack 来处理本地 vs 服务器。
.htaccess
file in /yii2app
dir:
.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>
Now in frontend and backend web directories, they both have the same .htaccess
:
现在在前端和后端 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
I do not want to see /frontend/web
or /backend/web
ever :)
我不想看到/frontend/web
或/backend/web
永远:)
I tried to play with the RewriteRule in the root's htaccess to add /admin
to the URL, but it kept telling me /admin
does not exist. I KNOWit does not exist, and I don't want it to exist. I want it to be a relative path.. ie: /admin == /backend/web.
我试图在 root 的 htaccess 中使用 RewriteRule 添加/admin
到 URL,但它一直告诉我/admin
不存在。我知道它不存在,我也不希望它存在。我希望它是一个相对路径.. 即:/admin == /backend/web。
Worded another way. I way everything in the project's root (http://localhost/yii2app/) to load frontend/web
, but without showing it. Also, http://localhost/yii2app/adminto load backend/web
and just showing http://localhost/yii2app/admin. Obviously they would have their respective controller/action
attached to them. So admin could look like http://localhost/yii2app/admin/site/login
换种说法。我将项目根目录 ( http://localhost/yii2app/) 中的所有内容加载frontend/web
,但没有显示。此外,http://localhost/yii2app/admin加载backend/web
并只显示http://localhost/yii2app/admin。显然,他们将各自controller/action
依附于他们。所以管理员可能看起来像http://localhost/yii2app/admin/site/login
NOTE: I have not played with any of the files. This is a stock yii2 advanced setup, using composer, and following the docs. The only thing I have played with so far are the htaccess files mentioned.
注意:我没有玩过任何文件。这是一个股票 yii2 高级设置,使用作曲家,并遵循文档。到目前为止,我唯一玩过的就是提到的 htaccess 文件。
Thank you!
谢谢!
采纳答案by deacs
If your only goal is to achieve not ever seeing /frontend/web
or /backend/web
, even without using .htaccess rules, you could go for the following:
如果您的唯一目标是实现从未看到/frontend/web
或/backend/web
,即使不使用 .htaccess 规则,您可以执行以下操作:
Why not just pull out the contents of the web
folders and place them in the root? Just adjust the path referring to the framework and config files in the entry scripts index.php
.
为什么不直接拉出web
文件夹的内容并将它们放在根目录中?只需在入口脚本中调整参考框架和配置文件的路径即可index.php
。
Your dir structure would look like:
您的目录结构如下所示:
- yii2app/
- frontend/
- backend/
- common/
- .. other folders..
- admin/
- assets/
- css/
- index.php
- assets/
- css/
- index.php
Your yii2app/index.php
would then look like:
你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();
and your yii2app/admin/index.php
would look like:
你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();
EDIT: your entry scripts could look different to mine, but you should get the idea of changing the paths to find the framework files with these examples.
编辑:您的入口脚本可能看起来与我的不同,但您应该了解更改路径以使用这些示例查找框架文件。
回答by mohit
Try this with .htaccess Method-
用 .htaccess 方法试试这个 -
Step 1
第1步
Create .htaccess file in root folder, i.e advanced/.htaccess
and write below code.
在根文件夹中创建 .htaccess 文件,即advanced/.htaccess
并编写以下代码。
Options +FollowSymlinks
RewriteEngine On
# deal with admin first
RewriteCond %{REQUEST_URI} ^/(admin) <------
RewriteRule ^admin/assets/(.*)$ backend/web/assets/ [L]
RewriteRule ^admin/css/(.*)$ backend/web/css/ [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/ [L]
RewriteRule ^css/(.*)$ frontend/web/css/ [L]
RewriteRule ^js/(.*)$ frontend/web/js/ [L]
RewriteRule ^images/(.*)$ frontend/web/images/ [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
Note: if you are trying in local server then replace ^/
with ^/project_name/
where you see arrow sign. Remove those arrow sign <------
after setup is done.
注意:如果您在本地服务器中尝试,请替换^/
为^/project_name/
您看到箭头符号的位置。<------
设置完成后删除那些箭头标志。
Step 2
第2步
Now create a components/Request.php
file in common directory and write below code in this file.
现在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();
}
}
}
Step 3
第 3 步
Installing component. Write below code in frontend/config/main.php
and backend/config/main.php
files respectively.
安装组件。分别在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,
],
Step 4 (Optional, if doesn't work till step three)
第 4 步(可选,如果在第三步之前不起作用)
create .htaccess file in web directory
在 web 目录中创建 .htaccess 文件
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/ [L]
Note: make sure you have enabled your mod rewrite in apache
注意:确保您已在 apache 中启用了 mod 重写
Thats it! You can try your project with www.project.com/admin
, www.project.com
就是这样!你可以尝试你的项目www.project.com/admin
,www.project.com
in local serverlocalhost/project_name/admin
, localhost/project_name
在本地服务器中localhost/project_name/admin
,localhost/project_name
回答by shivgre
Like @deacs said just move the files from frontend/web to yii2app (root folder) and create a folder in yii2app "admin" and move files from backend/web to yii2app/admin and then create .htaccess files in both admin and yii2app with following code :
就像@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
Then add/modify the urlManager component in config file main.php in both frontend/config/main.php and backend/config/main.php with following code :
然后在 frontend/config/main.php 和 backend/config/main.php 的配置文件 main.php 中添加/修改 urlManager 组件,代码如下:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'rules' => [
],
],
Then change the index.php in yii2app with following code :
然后使用以下代码更改 yii2app 中的 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__ . '/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();
Also change the index.php in yii2app/admin with following code :
还要使用以下代码更改 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();
Thats all you need to complete the SEO friendly URLs in yii2. I was struggling myself and then I got help from @deacs answer and I though maybe this will help someone.
这就是在 yii2 中完成 SEO 友好 URL 所需的全部内容。我自己也在挣扎,然后我从@deacs 的答案中得到了帮助,我虽然这可能会对某人有所帮助。
回答by despotbg
After 2 days searching for best solutions I make it this way (host on shared hosting).
I make subdomain admin.xxxxx.com
and point it to document root /public_html/xxxxxx/backend/web
Because shared hosting don't allow you to put custom document root for you main domain, I use this solution :
在寻找最佳解决方案 2 天后,我采用了这种方式(托管在共享主机上)。我创建子域admin.xxxxx.com
并将其指向文档根目录/public_html/xxxxxx/backend/web
因为共享主机不允许您为主域放置自定义文档根目录,所以我使用以下解决方案:
Change the primary domain name on your account, then add the old primary domain as an addon domain. This way, you can pick the root folder you want for the primary domain/new addon domain. (The new primary domain will now point to public_html.)
更改您帐户上的主域名,然后将旧的主域添加为附加域。这样,您可以为主域/新附加域选择所需的根文件夹。(新的主域现在将指向 public_html。)
Then point (now my addon domain) to right document root for frontend
/public_html/xxxxxx/frontend/web
然后将(现在是我的插件域)指向前端的正确文档根目录
/public_html/xxxxxx/frontend/web
回答by Aamir Sarwar
I followed "deacs" answer and got following error
我按照“deacs”回答并收到以下错误
Invalid Configuration – yii\base\InvalidConfigException
The directory does not exist: D:/wamp/www/yii2app/assets
then i created "assets" folder in "yii2app" and it works
然后我在“yii2app”中创建了“assets”文件夹,它可以工作
----------Second method------------------------
----------第二种方法------------------------
Without moving files you can follow following link
无需移动文件,您可以按照以下链接
https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/start-installation.md
https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/start-installation.md
----------Third method-----------------------------
----------第三种方法-----------------------------
http://www.yiiframework.com/doc-2.0/guide-tutorial-shared-hosting.html
http://www.yiiframework.com/doc-2.0/guide-tutorial-shared-hosting.html
回答by rob006
So far the easiest and the most flexible solution for me are symlinks. You can put project anywhere and only create symlinks to directories forced by your hosting. For example, put project in ~/domains/example.com/project
and create symlinks to public_html
directory.
到目前为止,对我来说最简单、最灵活的解决方案是symlinks。您可以将项目放在任何地方,并且只创建指向您的托管强制目录的符号链接。例如,将项目放入~/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
And there you have http://example.com/
with frontend and http://example.com/admin/
with backend.
你有http://example.com/
前端和http://example.com/admin/
后端。
If you need backend in separate domain (admin.example.com
):
如果您需要在单独的域 ( admin.example.com
) 中使用后端:
ln -s ~/domains/example.com/project/backend/web ~/domains/admin.example.com/public_html
Pros
优点
- No twisted rewrite rules that almost nobody understand.
- You don't have whole project in public directory, so some misconfiguration of webserver and/or PHP will no public your code and config files.
- You can adapt the project to any structure required by hosting.
- If you use relative symlinks, you can keep it under version control and create single webroot with all apps as subdirectories.
- 没有几乎没有人理解的扭曲重写规则。
- 您没有公共目录中的整个项目,因此网络服务器和/或 PHP 的一些错误配置将不会公开您的代码和配置文件。
- 您可以使项目适应托管所需的任何结构。
- 如果您使用相对符号链接,您可以将其置于版本控制之下,并使用所有应用程序作为子目录创建单个 webroot。
Cons
缺点
- If you have a hosting with some restricted
open_basedir
settings without any control over it, you may unable to do use it.
- 如果您的主机具有某些受限
open_basedir
设置而无法对其进行任何控制,则您可能无法使用它。