【问题标题】:.htaccess RewriteRule not working as i needed for my yii2 advance framework.htaccess RewriteRule 没有按照我的 yii2 高级框架的需要工作
【发布时间】:2019-03-22 21:44:47
【问题描述】:

我创建了这个 .htaccess 规则来显示前端和后端部分:

Options +FollowSymLinks
RewriteEngine On

# End the processing, if a rewrite already occurred
RewriteRule ^(frontend|backend)/web/ - [L]

# Handle the case of backend, skip ([S=1]) the following rule, if current matched
RewriteRule ^backend(/.*)?$ /backend/web/$1 [L]

# handle the case of frontend
RewriteRule .* /frontend/web/$0

# Uncomment the following, if you want speaking URL
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^([^/]+/web)/.*$ $1/index.php

backend 部分的规则(第二条规则)按照我需要的 url 像这样https://example.com/backend/web/site/login 但不加载后端登录页面,只显示前端主页。当用户使用后端作为https://example.com/backend 时,我想显示后端登录页面。但是,这不起作用。

当我更改如下规则时,规则会根据需要显示后端登录页面。但我只想在用户在 url 中输入后端时显示后端登录页面:

RewriteRule ^.*$ /backend/web/$1 [L]

这适用于本地端,但不适用于我的 1&1 IONOS 专用服务器端。

请帮我根据需要加载我的后端登录页面。

谢谢

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    => .htaccess

    Options -Indexes
    
    <IfModule mod_rewrite.c>
      RewriteEngine on  
      RewriteCond %{REQUEST_URI} /(uploadImages)
      RewriteRule ^uploadImages/(.*)$ uploadImages/$1 [L]
      RewriteCond %{REQUEST_URI} !^public
      RewriteRule ^backend(.+)?$ backend/web/$1 [L,PT]
      RewriteRule ^(.*)?$ frontend/web/$1 [L]
    </IfModule>
    
    # Deny accessing below extensions
    <Files ~ "(.json|.lock|.git)">
    Order allow,deny
    Deny from all
    </Files>
    # Deny accessing dot files
    RewriteRule (^\.|/\.) - [F]
    

    => 后端/config/main.php

    'request' => [
                'baseUrl' => '/backend', //point to your folder directly 
            ],
    'urlManager' => [
                'enablePrettyUrl' => true, // hide ?r=site%2Fsignup
                'showScriptName' => false, //hide frontend/web/index.php from URL            
                'rules' => [
                    '<controller:\w+>/<action:\w+>/<id:\w+>' => '<controller>/<action>',
                    '<alias:index|>' => 'site/<alias>',
                    '<alias:login|logout>' => 'site/<alias>', 
                ], 
            ],
    

    【讨论】:

    • 根据您的后端/config/main.php,它不会从站点控制器重定向到我的登录页面,仍然是同样的问题。
    • 你能分享我们'request' => ['baseUrl' => '/backend', //直接指向你的文件夹],后端/控制器/站点控制器的代码以及你真正想要的输入网址,应该加载什么?
    • 我的 'request' => [ 'csrfParam' => '_csrf-backend', 'baseUrl' => '/backend', ] 和 'urlManager' => [ 'rules' => [ '//' => '/', ], ],我在我的站点控制器中有操作索引和登录.当我输入 backend 显示前端相同页面而不是后端登录时,其他详细信息有问题。
    • 和我上面建议的你正在使用的相同的 .htaccess。只是为了确认,如果您在本地工作,那么您必须使用 baseurl 作为项目文件夹名称/后端。那么 url 应该是 localhost/projectfoldername/backend.
    • 我正在使用我在问题中提到的 htaccess,我在服务器上的项目目录路径是 /projectName/backend
    猜你喜欢
    • 1970-01-01
    • 2017-07-13
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多