Website Building » Shopify » How Do I Redirect WWW to Non WWW in Shopify?

How Do I Redirect WWW to Non WWW in Shopify?

Last updated on January 13, 2023 @ 2:50 am

It’s really easy to redirect www to non www in Shopify. You just need to add a code snippet to your theme’s functions.php file. Here’s the code snippet:

function wp_redirect_nonwww(){
if (strpos($_SERVER['HTTP_HOST'], 'www.') !== false) {
$host = str_replace('www. 

', '', $_SERVER['HTTP_HOST']);
header('Location: http://' . $host . $_SERVER['REQUEST_URI']);
exit;
}
}
add_action('init', 'wp_redirect_nonwww');

Once you add that code snippet, www will be redirected to non www automatically.

PRO TIP: If you are not careful, redirecting www to non www in Shopify can break your store. Make sure to test your store thoroughly after making this change to make sure everything is still working as expected.
Drew Clemente

Drew Clemente

Devops & Sysadmin engineer. I basically build infrastructure online.