.htaccess Telegram Webhook在yii2中返回302错误

but5z9lq  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(153)

我不知道如何在yii 2中配置地址,使电报不返回302错误
在根目录中,在/frontend中。htaccess文件这些设置

Options +FollowSymLinks

IndexIgnore /
RewriteEngine on

RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin\/?(.*) /backend/web/$1

RewriteCond %{REQUEST_URI} !^/(frontend/web|backend/web|admin|site/webhook)
RewriteRule (.*) /frontend/web/$1 [L]

RewriteCond %{REQUEST_URI} ^/frontend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /frontend/web/index.php [L]

RewriteCond %{REQUEST_URI} ^/backend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /backend/web/index.php [L]

在前端的SiteController中

public function actionSetWebhook()
{
    Yii::$app->telegram->setWebhook([
        'url' => Yii::$app->urlManager->createAbsoluteUrl(['site/webhook']),
    ]);

}
public function actionWebhook()
{
    $data = file_get_contents('php://input');
    $data = json_decode($data, true);
}

webhook安装无误,但当使用getWebhookInfo方法发送消息时,电报给出以下错误

{"ok":true,"result":{"url":"https://домен/site/webhook","has_custom_certificate":false,"pending_update_count":35,"last_error_date":1680856667,"last_error_message":"Wrong response from the webhook: 302 Found","max_connections":40,"ip_address":"айпи"}}

runtime/logs/app.log

2023-04-07 11:37:47 [айпи][-][-][info][application] $_GET = [
    'r' => 'site/webhook'
]

$_POST = []

$_FILES = []

$_COOKIE = []

$_SERVER = [
    'REDIRECT_HTTPS' => 'on'
    'REDIRECT_downgrade-1_0' => ''
    'REDIRECT_suppress-error-charset' => ''
    'REDIRECT_STATUS' => '200'
    'HTTPS' => 'on'
    'downgrade-1_0' => ''
    'suppress-error-charset' => ''
    'HTTP_HOST' => 'домен'
    'HTTP_X_REAL_IP' => 'айпи'
    'HTTP_X_PROTOCOL' => 'HTTP/1.1'
    'HTTP_X_SERVER_IP' => 'айпи'
    'HTTP_X_FORWARDED_PROTOCOL' => 'https'
    'HTTP_X_FORWARDED_PROTO' => 'https'
    'HTTP_CONNECTION' => 'close'
    'CONTENT_LENGTH' => '444'
    'CONTENT_TYPE' => 'application/json'
    'HTTP_ACCEPT_ENCODING' => 'gzip, deflate'
    'PATH' => '/usr/bin:/bin:/usr/local/bin'
    'SERVER_SIGNATURE' => ''
    'SERVER_SOFTWARE' => 'Apache'
    'SERVER_NAME' => 'домен'
    'SERVER_ADDR' => 'айпи'
    'SERVER_PORT' => '80'
    'REMOTE_ADDR' => 'айпи'
    'DOCUMENT_ROOT' => '/home/users/o/аккаунт/domains/домен'
    'REQUEST_SCHEME' => 'http'
    'CONTEXT_PREFIX' => ''
    'CONTEXT_DOCUMENT_ROOT' => '/home/users/o/аккаунт/domains/домен'
    'SERVER_ADMIN' => 'info@jino.ru'
    'SCRIPT_FILENAME' => '/home/users/o/аккаунт/domains/домен/frontend/web/index.php'
    'REMOTE_PORT' => '36426'
    'REDIRECT_URL' => '/site/webhook'
    'REDIRECT_QUERY_STRING' => 'r=site/webhook'
    'GATEWAY_INTERFACE' => 'CGI/1.1'
    'SERVER_PROTOCOL' => 'HTTP/1.0'
    'REQUEST_METHOD' => 'POST'
    'QUERY_STRING' => 'r=site/webhook'
    'REQUEST_URI' => '/site/webhook'
    'SCRIPT_NAME' => '/frontend/web/index.php'
    'PHP_SELF' => '/frontend/web/index.php'
    'REQUEST_TIME_FLOAT' => 1680856667.8266
    'REQUEST_TIME' => 1680856667
]

如果webhook设置为一个在站点根目录下的文件(不是在前端),我就得到了数据,但是我需要在数据库中添加一条记录,而从父目录下是做不到的

eoigrqb6

eoigrqb61#

实际上,您希望site/webhookSiteController处理,因此您需要将RewriteRule添加到Yii2应用程序中。只要更新你的线路

RewriteCond %{REQUEST_URI} !^/(frontend/web|backend/web|admin|site/webhook)

RewriteCond %{REQUEST_URI} !^/(frontend/web|backend/web|admin)

然后加上

RewriteCond %{REQUEST_URI} ^/site/webhook
RewriteRule . /frontend/web/index.php [L]

如果它是后端的一部分,则更新此新规则以重定向到它。如果你想让Yii2代码处理请求,应该有一个规则将它重定向到index.php文件,并调用控制器动作。

相关问题