how to re-direct non-www domain to www.domain.com in Wordpress App Service

Aj 0 Reputation points
2024-04-20T07:47:11.1033333+00:00

I have setup the domain (let's call it www.bob.com) as a custom domain for a Wordpress App Service.

www.bob.com is pointing to the CNAME record generated by the App Service. and bob.com is pointing to the IP address generated by App Service. (this is configured in Google Cloud DNS)

I want http://bob.com and https://bob.com to redirect to https://www.bob.com

How can I achieve this?

I have tried setting the nginx [dot] conf file in the wwwroot directory and it does nothing. Same with adding a redirect rule in the [dot] htaccess file , and any time I modify the /etc/nginx/conf.d/default.conf file, it just gets overwritten when I restart the app.

Any advice would be greatly appreciated. Thanks.

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

1 answer

Sort by: Most helpful
  1. Grmacjon-MSFT 16,101 Reputation points
    2024-04-25T05:47:07.9033333+00:00

    Hello @Aj to achieve your redirection for http://bob.com, https://bob.com, and https://www.bob.com, you can follow these steps:

    1. In the WordPress admin dashboard, go to "Settings" > "General" and ensure that the "WordPress Address (URL)" and "Site Address (URL)" fields are set to "https://www.bob.com".
    2. If you're using an Application Gateway or Front Door service in Azure, you can set up URL redirection rules there. This is generally the recommended approach as it offloads the redirection logic from the App Service instance.
    • For Application Gateway, follow the instructions in the Azure documentation to create a rewrite rule that redirects HTTP to HTTPS and non-www to www.
    • For Front Door, follow the instructions in the Azure documentation to create a rewrite rule for the desired redirection.
    1. If you don't have an Application Gateway or Front Door, you can configure the redirection within the Web App itself. However, this approach is less recommended as it adds overhead to the App Service instance. a. Create a web.config file in the root directory of your WordPress installation (same level as wp-config.php). b. Add the following code to the web.config file:
         <?xml version="1.0" encoding="UTF-8"?>
         
         <configuration>
         
             <system.webServer>
         
                 <rewrite>
         
                     <rules>
         
                         <rule name="Redirect to HTTPS" stopProcessing="true">
         
                             <match url="(.*)" />
         
                             <conditions>
         
                                 <add input="{HTTPS}" pattern="^OFF$" />
         
                             </conditions>
         
                             <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
         
                         </rule>
         
                         <rule name="Redirect to www" stopProcessing="true">
         
                             <match url="(.*)" />
         
                             <conditions>
         
                                 <add input="{HTTP_HOST}" pattern="^bob\.com$" />
         
                             </conditions>
         
                             <action type="Redirect" url="https://www.bob.com/{R:1}" redirectType="Permanent" />
         
                         </rule>
         
                     </rules>
         
                 </rewrite>
         
             </system.webServer>
         
         </configuration>
      
      This web.config file contains two rules:
      • The first rule redirects HTTP traffic to HTTPS.
      • The second rule redirects non-www traffic (bob.com) to www (www.bob.com).

    After making these changes, your website should be accessible via https://www.bob.com, and any requests to http://bob.com or https://bob.com should be redirected to https://www.bob.com.

    Note: If you're using an Application Gateway or Front Door, you can skip step 3 and rely on the redirection rules set up in those services. Hope that helps.

    -Grace

    0 comments No comments