【问题标题】:Yii2 | Redirect to backend/frontend with htaccessYii2 |使用 htaccess 重定向到后端/前端
【发布时间】:2019-08-15 23:21:41
【问题描述】:

我在localhost 上使用Yii2 高级模板,当url 为localhost/site/admin 时,我想将用户重定向到backend/web,并在url 为localhost/site 时使用.htaccess 文件重定向到frontend/web

我启用了apache rewrite,并尝试了这些解决方案
advance template Yii2 - redirect to frontend/web using htaccess
Yii2 .htaccess redirect to backend part
Redirecting to backend via htaccess Yii2

但在所有情况下,当 url 为 localhost/site/admin 时,我都会得到 404 Error

未找到

在此服务器上找不到请求的 URL /site/admin。

当 url 为 localhost/site 时,我的站点根目录的内容会显示给我。

现在我在项目根目录中的.htaccess 是:

# prevent directory listings
    Options -Indexes
    IndexIgnore */*

    # follow symbolic links
    Options FollowSymlinks
    RewriteEngine on
    RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]
    RewriteRule ^(.+)?$ frontend/web/$1

backend/webfrontend/web 中是:

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

【问题讨论】:

  • 我不确定,但我相信RewriteRule ^(.+)admin(/.+)?$ backend/web/$1 [L,PT] 应该可以。我认为您与上述规则匹配 localhost/admin。请告诉我进展如何。
  • 创建 2 个虚拟主机,排除.htaccess 让您的生活更轻松,请参阅here
  • @MartinDimitrov 最后,我注意到虽然我通过终端启用了 ‍mod_rewrite,但配置文件没有更改,我手动进行了更改。
  • @MuhammadOmerAslam 感谢您的建议,但该站点必须位于共享主机上,并且我无权构建虚拟主机
  • 如果您在实时服务器上,您可以使用symlink 或前端和后端或遵循此github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/…

标签: apache .htaccess yii2 yii2-advanced-app


【解决方案1】:

虽然我已经通过终端通过以下命令启用了mod_rewrite

sudo a2enmod rewrite

但它没有启用,我从 ‍/etc/apache2/apache2.conf 手动激活它。

【讨论】:

    【解决方案2】:

    在没有site的情况下使用它

    localhost/admin
    

    【讨论】:

      猜你喜欢
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多