Redirecting non-www to www or www to non-www on WordPress in Azure App service could be an issue if you don’t know what components are installed when you use the WordPress for Azure app service by Microsoft.

When WordPress sites are installed, the site needs to be served on one URL for many reasons such as to reduce discrepancies in development, SEO issues with duplicate content and general performance issues. Some would prefer the site to be loaded with www or non-www.
If your hosting provider uses Apache server then you can utilise the .htaccess file to do the redirection.

If you have hosted a WordPress site in Azure App Service using the Microsoft offering, it deploys a burstable B series Linux Virtual machine with Nginx and Azure for MySQL database.

In this quick guide, we will look at resolving the redirection issue in your WordPress site hosted in Azure app services.

High level:
We want to change the configuration on nginx which points to our site. Add a snippet of code so any request from the site is forced to redirect to how we wanted. Finally, update the startup script so the change we did will be permanent.

Now let’s look at the detailed steps:

Step 1:

Go to your desired app and select ‘SSH’ under development tools:

Step 2:

We need to copy the default configration from root to the site folder to edit it. Run the following commands in SSH.

cp /etc/nginx/conf.d/default.conf /home/site/default

Step 3:

Now go the file Manager so the config file you moved can be edited on the GUI.
This time click on the ‘Advanced Tools’ under ‘Development Tools’ and click ‘Go’
It will load the SCM site, enter a forward slash and type ‘newui’ at the end of the URL so the full URL will look like below.

https://<your app name>.scm.azurewebsites.net

Step 4:

Click on File Manager —> Site then edit the ‘default’ file.

Step 5:

You will see the server block as shown below. update the desired URL of the website next to ‘server_name… ‘ line. This could be the URL with or without www whichever you want as the single URL that the user should end up with.

server {
        listen 80;
        ## Your website name goes here.
        server_name <your website URL>;
        ## Your only path reference.
        root /var/www/wordpress;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

Step 6:

Enter another block of server on top of the existing server block so you have two server blocks now. in this new server block, you enter the redirecting parameters.

For non-www to www redirection

server {

        listen 80;

        server_name www.yoursite.com;

        return 301 $scheme://www.yoursite.com$request_uri;
}

For www to non-www redirection

server {

    listen 80;

    server_name www.yoursite.com;

    return 301 $scheme://yoursite.com$request_uri;

}

Save the changes and close the config file.

Step 7:

Click on File Manager —> dev —> startup.sh. click edit file and when he file is open copy the below code lines so the changes are applied

cp /home/site/default /etc/nginx/conf.d/default.conf
/usr/sbin/nginx -s reload

Step 8:

Restart your app service and test the redirection.

Leave a Reply

Your email address will not be published. Required fields are marked *