【问题标题】:yii2-app-advanced on single domain not work for meyii2-app-advanced 在单个域上对我不起作用
【发布时间】:2017-08-19 16:15:04
【问题描述】:

我根据yii official wiki 配置了我的 yii2 advanced,以便在 localhost 的单个域中使用后端和前端。 我用硬方法修改文件

frontend/config/main.php

backend/config/main.php

并添加

.htaccess with mod_rewrite

根据维基。 当我转到http://localhost/ 时,一切正常。但是当我转到http://localhost/site/about 和其他链接时,显示浏览器

Object not found!

当我想去http://localhost/backend/时,浏览器将我重定向到http://localhost/backend/site/login并显示

Object not found!

再次。 我在过去的 yii2 项目中使用了这个 wiki,没有任何问题。但现在我不能使用。 我怎么了? 我是伊朗人,我的英语不是很好。请原谅我的语言不好。

【问题讨论】:

    标签: php .htaccess mod-rewrite yii2 yii2-advanced-app


    【解决方案1】:

    我的问题已经解决了。 根据this answerbackend/webfrontend/web添加了额外的.htaccess文件,内容如下:

    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
    

    【讨论】:

      【解决方案2】:

      我在遵循 yii2 hard way 模板时遇到了同样的问题,然后我在 config、vhost 和 .htaccess 文件中进行了以下更改。

      /frontend/config/main.php

      return  [
              'homeUrl' => '/',
              'components' => [
                  'request' => [
                      'baseUrl' => '',
                  ],
                  'urlManager' => [
                      'enablePrettyUrl' => true,
                      'showScriptName' => false,
                  ],
              ],
          ];
      

      /backend/config/main.php

      return  [
              'homeUrl' => '/admin',
              'components' => [
                  'request' => [
                      'baseUrl' => '/admin',
                  ],
                  'urlManager' => [
                      'enablePrettyUrl' => true,
                      'showScriptName' => false,
                  ],
              ],
          ];
      

      .htaccess 您需要在应用程序根目录中的 .htaccess 文件中添加下面提到的代码。如果您没有,则需要在高级应用程序的根目录中创建 .htaccess 文件。

      Options FollowSymLinks
      AddDefaultCharset utf-8
      
      <ifmodule mod_rewrite.c>
      RewriteEngine On
      
      # the main rewrite rule for the frontend application
      RewriteCond %{REQUEST_URI} !^/(backend/web|admin)
      RewriteRule !^frontend/web /frontend/web%{REQUEST_URI} [L]
      
      # redirect to the page without a trailing slash 
      #RewriteCond %{REQUEST_URI} ^/admin/$
      #RewriteRule ^(admin)/ /$1 [L,R=301]
      # the main rewrite rule for the backend application
      RewriteCond %{REQUEST_URI} ^/admin
      RewriteRule ^admin(.*) /backend/web/$1 [L]
      
      # if a directory or a file of the frontend application exists, 
      # use the request directly
      RewriteCond %{REQUEST_URI} ^/frontend/web
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      # otherwise forward the request to index.php
      RewriteRule . /frontend/web/index.php [L]
      
      # if a directory or a file of the backend application exists, 
      # use the request directly
      RewriteCond %{REQUEST_URI} ^/backend/web
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      # otherwise forward the request to index.php
      RewriteRule . /backend/web/index.php [L]
      
      RewriteCond %{REQUEST_URI} \.(htaccess|htpasswd|svn|git)
      RewriteRule \.(htaccess|htpasswd|svn|git) - [F]
      </ifmodule>
      

      虚拟主机文件

      <VirtualHost *:80>
      DocumentRoot "path of your project" //no need to mention backend/frontend
      ServerName "example.com"
      <Directory "path of your project">
          Options Indexes FollowSymLinks MultiViews
          AllowOverride All
          Require all granted
      </Directory>
      

      【讨论】:

        猜你喜欢
        • 2017-02-16
        • 1970-01-01
        • 2016-06-04
        • 2020-01-04
        • 2015-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多