The following EdgeRule will redirect traffic from your www subdomain (www.example.com) to your apex domain (example.com), and HTTP to HTTPS protocol with just one 301 redirect. This article can also be used to redirect other subdomains like support.example.com or blog.examples.com.
Creating the Rule
- Log into the StackPath Control Portal
- From the Dashboard, select the Site you would like to create the rule for
- Navigate to the EdgeRules tab
- Select Add Delivery Rule
- In the initial IF Option, please select the URL option and select the Matches setting and add-in
- In the THEN option, select Redirect and add
Remember to replace "yourdomain" with the actual domain of the website.
By setting a URL Matches condition to match
*://www*, the EdgeRule will check to see if the incoming request contains WWW at the beginning of the URL before triggering the rule. The "THEN: Redirect" will redirect the incoming request to use
https:// at the beginning of the URL to force the HTTPS protocol, and remove WWW from the URL. Additionally, it will add the Full File Path and Query String Parameters from the request with
Verifying The Rule
The rule is intended to perform one 301 for your domain. You can verify this by checking the headers from CDN assets using the inspect tools or cURL requests.
- To view the browser tools, right-click anywhere on the website and select inspect or inspect element option from the menu. Then, navigate to the Network tab. If you do not see any requests, re-request the WWW version of your website by hitting the refresh button and watch the requests come in.
- When performing a cURL test, the options
-Iis important to include in order to see the headers appropriately and
-Lto follow the redirect automatically. Below is an example of a curl, where you can see the 301 status code and then a 200 without WWW added at the beginning of the domain.
If any questions arise or any assistance can be further provided, feel free to drop us a line at email@example.com or open up a chat with our 24/7 live support, we'll be happy to help you out.