【发布时间】: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/web 和frontend/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