How to solve 307 Temporary Redirect error in Azure App

Simon Lovejoy 0 Reputation points
2024-06-23T06:52:31.1833333+00:00

I have encountered a 307 Temporary Redirect alert on my website, which is being caused by a redirect between http:// and https://.

This then appears to be contributing to another alert, which states that the 'home page appears to have no server-side 301 redirect from WWW to non-WWW website version or vice versa'. However I have set up a Rule in the app service with a 301 to redirect http content to https, and so I am uncertain as to whether there is an issue with this.

I am not very familiar with Azure architecture, unfortunately, and do not know exactly where to look for a configuration setting to fix this issue - I have audited the app portal interface as best I can but this has not revealed any answers up to now.

Is there a setting I can use within the app portal to address this problem?

Additionally, there is a web.config file in the root FTP directory of the app that i can access if this needs to be modified as I understand may be the case.

Many thanks in advance for any assistance.

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,421 questions
{count} votes

1 answer

Sort by: Most helpful
  1. ajkuma 24,971 Reputation points Microsoft Employee
    2024-06-24T08:57:00.0266667+00:00

    @Simon Lovejoy , Apologies for the delayed response from over the weekend.

    Azure App Service on Linux images using PHP 8.x are now bundled with NGINX instead of Apache. The use of htaccess files will not work for NGINX as these are used for Apache only. This will require the need to setup a custom startup script and modifying the existing NGINX site configuration.

    Just to clarify, is this a built-in/blessed image or a custom docker image? What is the application framework?

    Navigate to App Service Web SSH via https://<YourWebAppName>.scm.azurewebsites.net/webssh/host

    • You will want to make a copy of the existing configuration and place the file inside the /home/site directory.
    • You will now need to create a custom startup script and save the file as /home/site/startup.sh
    • Reloading the NGINX service to make the updates take effect.

    See this article for step-step instructions: Configure Nginx for PHP 8 Linux Azure App Service

    Depending on the framework you are using, on App service in general, (as an example ) Node App Service on Linux that the environment is a Docker Container which includes Node itself (along with your code) and also PM2, a production process manager - called our “Blessed Images”.

    If using something like Expressjs and have your controllers set up for routing, the below approach can be used, which is using an exported function that we can pass back into our routing middleware. req, res, next will be available since this function can be passed as an argument directly to Express middleware. This example is redirecting from non-www to www:

    const redirectController = (req, res, next) => {
            try {
              const host = req.headers.host;
              if (host.match(/^www\..*/i)) {
                next();
              } else {
                res.redirect(301, `${req.protocol}://www.` + host + req.url);
              }
            } catch (error) {
              console.log("An error has occurred: ", error);
              next(error);
            }
          };
    
          module.exports = redirectController;
    
    

    Refer to NGINX Rewrite Rules for Azure App Service Linux PHP 8.x for details.

    Kindly let us know, I'll follow-up with you further.