简体   繁体   English

Php HTTP 重定向不适用于 Azure 应用服务

[英]Php HTTP redirect not working on Azure App Service

I'm trying to redirect my user to a certain website when they visit one of my end point.当我的用户访问我的一个端点时,我试图将他们重定向到某个网站。 https://url-shortner-in.azurewebsites.net/openfb will point to google.com https://url-shortner-in.azurewebsites.net/openfb将指向 google.com

My server is hosted on Azure App Service with Slim Framework v4 using REST API.我的服务器使用 REST API 托管在带有 Slim Framework v4 的 Azure 应用服务上。

This is my call to redirect the user.这是我重定向用户的电话。

return $response
            ->withHeader('Location', 'http://www.google.com')
            ->withStatus(302);

It's working fine on my local WAMP server.它在我的本地 WAMP 服务器上运行良好。 I'm getting redirected to Google.我被重定向到谷歌。

But after uploading it on Azure App Service, the code isn't working.但是在 Azure 应用服务上上传后,代码不起作用。 I'm seeing a blank page after redirection.重定向后我看到一个空白页面。 The URL stays the same (my website URL and not Google's.) URL 保持不变(我的网站 URL 而不是 Google 的。)

Is there any misconfiguration with Azure? Azure 是否存在任何配置错误?

My .htaccess我的.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

I suggest you choose windows platform when create webapps.我建议你在创建 webapp 时选择 windows 平台。

You can try to create web.config file under the wwwroot folder, if it doesn't exist.您可以尝试在 wwwroot 文件夹下创建web.config文件,如果它不存在。 If you can find this file when deployed your webapp, you need to modify it.如果你在部署你的 webapp 时能找到这个文件,你需要修改它。

The specific content to be added or modified is that RewriterConfig needs to be added to web.config.具体需要添加或修改的内容是需要在web.config中添加RewriterConfig

The format is as follows:格式如下:

<configSections>
 <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler,URLRewriter" />
</configSections>
......
<RewriterConfig>
   <Rules>
      <RewriterRule>
        <LookFor>^default/([0-9]+)/([_0-9a-z-]+)</LookFor>
        <SendTo>11.aspx?id={R:1}</SendTo>
      </RewriterRule>
   </Rules>
</RewriterConfig>

For more details, you can refer to the following two posts:更详细的可以参考以下两篇文章:

Azure Web App Angular Not redirecting to www Azure Web App Angular 不重定向到 www

DNN UrlRewrite (“DotNetNuke.HttpModules.UrlRewriteModule, DotNetNuke.HttpModules”) does not run custom rewrite rule on web.config DNN UrlRewrite(“DotNetNuke.HttpModules.UrlRewriteModule,DotNetNuke.HttpModules”)不会在 web.config 上运行自定义重写规则

Step 1 : Enable Apache .htaccess第 1 步:启用 Apache .htaccess

By default, the .htaccess file is not enabled.默认情况下,不启用 .htaccess 文件。

1. Open the default host configuration file by entering the following command in the terminal: 1.通过在终端中输入以下命令打开默认主机配置文件:

sudo nano /etc/apache2/sites-available/default

2. Locate the section labeled <Directory /var/www> . 2.找到标有<Directory /var/www> In that section, change the AllowOverride None entry to all :在该部分中,将AllowOverride None条目更改为all

AllowOverride All

Save the file and exit.保存文件并退出。

3. Next, restart the Apache service: 3.接下来,重启Apache服务:

sudo systemctl apache2 restart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM