简体   繁体   English

Yii2 htaccess - 如何完全隐藏前端/网络和后端/网络

[英]Yii2 htaccess - How to hide frontend/web and backend/web COMPLETELY

I think I am pretty close.我觉得我很接近。 I have the htaccess redirecting to the website (frontend/web) and the /admin path ( backend/web ).我将 htaccess 重定向到网站(前端/网络)和/admin路径( backend/web )。 The site appears fine, CSS files loading, etc.该站点显示正常,加载 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: /yii2app目录中的.htaccess文件:

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.我试图在 root 的 htaccess 中使用 RewriteRule 将/admin添加到 URL,但它一直告诉我/admin不存在。 I KNOW it does not exist, and I don't want it to exist.知道它不存在,我也不希望它存在。 I want it to be a relative path.. ie: /admin == /backend/web.我希望它是一个相对路径.. 即:/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.我使用项目根目录 ( http://localhost/yii2app/ ) 中的所有内容加载frontend/web ,但没有显示它。 Also, http://localhost/yii2app/admin to load backend/web and just showing http://localhost/yii2app/admin .另外, http://localhost/yii2app/admin加载backend/web并只显示http://localhost/yii2app/admin Obviously they would have their respective controller/action attached to them.显然,他们会将各自的controller/action附加到他们身上。 So admin could look like http://localhost/yii2app/admin/site/login所以管理员可能看起来像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.这是一个股票 yii2 高级设置,使用作曲家,并遵循文档。 The only thing I have played with so far are the htaccess files mentioned.到目前为止,我唯一玩过的就是提到的 htaccess 文件。

Thank you!谢谢!

Try this with .htaccess Method-用 .htaccess 方法试试这个 -

Step 1第1步

Create .htaccess file in root folder, ie 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/$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

Note : if you are trying in local server then replace ^/ with ^/project_name/ where you see arrow sign.注意:如果您在本地服务器中尝试,则将^/替换为^/project_name/看到箭头符号的位置。 Remove those arrow sign <------ after setup is done.设置完成后删除那些箭头符号<------

Step 2第2步

Now create a components/Request.php file in common directory and write below code in this file.现在在 common 目录中创建一个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.phpbackend/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?/$1 [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 server在本地服务器
localhost/project_name/admin , localhost/project_name localhost/project_name/adminlocalhost/project_name

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?为什么不直接拉出web文件夹的内容并将它们放在根目录中? Just adjust the path referring to the framework and config files in the entry scripts index.php .只需在入口脚本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.编辑:您的入口脚本可能看起来与我的不同,但您应该了解更改路径以使用这些示例查找框架文件。

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.这就是在 yii2 中完成 SEO 友好 URL 所需的全部内容。 I was struggling myself and then I got help from @deacs answer and I though maybe this will help someone.我自己也在挣扎,然后我从@deacs 的答案中得到了帮助,我虽然这可能会对某人有所帮助。

After 2 days searching for best solutions I make it this way (host on shared hosting).在寻找最佳解决方案 2 天后,我采用了这种方式(托管在共享主机上)。 I make subdomain admin.xxxxx.com and point it to document root /public_html/xxxxxx/backend/web我创建子域admin.xxxxx.com并将其指向文档根目录/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 :由于共享主机不允许您为主域放置自定义文档根目录,因此我使用以下解决方案:

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右文档根目录

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

So far the easiest and the most flexible solution for me are symlinks .到目前为止,对我来说最简单、最灵活的解决方案是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.例如,将项目放在~/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优点

  1. No twisted rewrite rules that almost nobody understand.没有几乎没有人理解的扭曲重写规则。
  2. 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.您没有公共目录中的整个项目,因此网络服务器和/或 PHP 的一些错误配置将不会公开您的代码和配置文件。
  3. You can adapt the project to any structure required by hosting.您可以使项目适应托管所需的任何结构。
  4. If you use relative symlinks, you can keep it under version control and create single webroot with all apps as subdirectories.如果您使用相对符号链接,您可以将其置于版本控制之下,并使用所有应用程序作为子目录创建单个 webroot。

Cons缺点

  1. 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设置而无法对其进行任何控制,则您可能无法使用它。

put .htaccess in web dir将 .htaccess 放在 web 目录中

code代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM