Magento 2 Change Base URL Through CLI and Database

Magento 2 Change Base URL Through CLI and Database

Struggling with broken links and admin access issues? Magento 2 change base URL procedures often create problems when they go wrong.

In this article, we will explain the right way to change the base URL and address issues.

3 min read Critical for store migration

Is Your Magento Store URL Change
About to Break Everything?

Watch what happens when you change your base URL without proper preparation...

https://oldstore.com
87%

Stores break on
first URL change

4-8h

Average downtime
from URL errors

5min

To change URL
the right way

🚨 Don't let this happen to your store!

Keep reading to learn the safe way to change your Magento 2 base URL...

Listen to This Article

Audio guide for Magento 2 base URL changes

0:000:00

Audio Summary: This guide covers changing Magento 2 base URLs through CLI and database methods, troubleshooting common issues, and best practices for URL migration.

Key Takeaways

  • Base URL settings control your web address and how customers access your store.

  • Four methods change base URLs through the admin panel, database, prompts, and files.

  • Backup needs include saving your database and files before making changes.

  • Common problems include broken styles, missing images, and admin access issues.

  • Proper testing ensures your store is functioning and redirects customers.

What is the Base URL in Magento 2?

Types of Magento Base URLs on the Platform

Hover over each type to see live examples and understand their purpose

Unsecure Base URL

Handles standard web traffic for non-sensitive pages

http://yourstore.com/
Use Cases:
  • Product browsing
  • Category pages
  • CMS content
SECURE

Secure Base URL

Manages encrypted traffic for sensitive transactions

https://yourstore.com/
Use Cases:
  • Checkout process
  • Customer login
  • Payment pages

Unsecure Base Link URL

Controls internal link generation for non-secure pages

{{unsecure_base_url}}
Purpose:
  • Internal routing
  • Dynamic links
  • System variables

Pro Tip: Always update both secure and unsecure URLs together

Mismatched URL configurations are the #1 cause of redirect loops and broken stores.

The base URL serves as the foundational web address. Magento uses to construct all links throughout your online store. This configuration setting indicates the location of your store on the internet. It also tells how customers and search engines can access it.

Key functions of the base URL include:

  1. Generates all frontend links for product pages, categories, and CMS pages.

  2. Creates back-end admin panel URLs for store management.

  3. Constructs media URLs for Magento images, logos, and other assets.

  4. Forms the foundation for SEO friendly URLs and sitemap generation.

  5. Enables proper redirect functionality and canonical URL structure.

Types of Magento Base URLs on the Platform

Base URL Type Primary Purpose Use Cases
Unsecure Base URL Handles standard web traffic for non-sensitive pages Product browsing, category pages, CMS content, and general navigation
Secure Base URL Manages encrypted traffic for sensitive transactions Checkout process, customer login, payment pages, account management
Unsecure Base Link URL Controls internal link generation for non-secure pages Magento navigation menus, breadcrumbs, category links, and product URLs
Secure Base Link URL Determines link generation for secure page connections Checkout navigation, secure area linking, encrypted page transitions

Steps to Change the Base URL Via CLI & Tables

Steps to Change the Base URL Via CLI & Tables

Interactive terminal - Click to type commands and see real-time execution

1Via CLI

magento@store:~/public_html$
$bin/magento config:set web/unsecure/base_url https://newstore.com/

2Via Database

MySQL Query
UPDATE core_config_data 
SET value = 'https://newstore.com/' 
WHERE path IN(
    'web/secure/base_url', 
    'web/unsecure/base_url'
);

Warning: Direct database changes

Always backup your database before running SQL queries. Clear cache after changes.

2 Commands

CLI method requires

30 Seconds

Average execution time

Always Flush

Cache after changes

1. Via CLI

  1. Open the CLI from your admin panel and enter the prompts:
    bin/magento config:set web/unsecure/base\_url \[invalid url, do not cite\]

    bin/magento config:set web/secure/base\_url \[invalid url, do not cite\]

2. Via Tables

  1. Open the Magento 2 database and enter the following prompts:

    UPDATE core\_config\_data SET value \= '\[invalid url, do not cite\]' WHERE path IN('web/secure/base\_url', 'web/unsecure/base\_url');

When Do You Need to Change the Magento Base URL?

When Do You Need to Change the Magento Base URL?

Click through this interactive decision tree to find your scenario

Do I Need to Change My Base URL?

Click to explore common scenarios

1. Domain Migration Scenarios

Magento 2 change base URL due to Domain migration scenarios

  • Business owners need to change their store's domain name for various reasons. Companies rebrand and need a new domain that matches their updated identity. Organizations buy premium domain names to improve their online presence and trust.

  • Website owners also migrate domains when getting many sites into a single platform. They transfer ownership of existing Magento stores to new companies. These operate under different domain names.

2. Moving from Staging to Production

  • Developers create staging environments using temporary domains for testing. These staging sites use URLs like staging.yourstore.com during development. Teams test new features, extensions, and customizations on these temporary environments.

  • Once testing completes, developers must update the base URL to point to the live domain. This transition ensures customers access the the correct public domain.

  • The staging-to-production migration also involves updating all internal links and configurations. It is to match the final website address.

3. HTTPS Implementation

  • Modern ecommerce needs secure connections. It protects customer data and maintains search engine rankings.

  • Store owners use SSL certificates to enable HTTPS encryption across their entire website. Google prioritizes secure websites in search results. It makes HTTPS essential for SEO performance.

  • Businesses must update their base URL configuration. It is when transitioning from HTTP to HTTPS protocols. This change affects both secure and unsecure URL settings throughout the installation. Payment processors and modern browsers also need HTTPS for safe transactions.

4. Subdirectory Changes

Magento 2 change base URL due to subdirectory changes

  • Some businesses install Magento in subdirectories from the start. Companies later decide to move their store to the root domain. It improves SEO and simplifies navigation. This migration needs to update the base URL to remove the subdirectory path.

  • Businesses sometimes need to move their store into a subdirectory. It is to accommodate other website content. Companies might add blogs, corporate pages, or other applications to their root domain.

5. Multi-Store Configurations

  • Businesses expand their operations by creating many store views. These are for countries, languages, or product lines.

  • Each store view often needs its domain or subdomain to serve customer segments. Companies might use domains like us.yourstore.com for customers in the United States. Or domains like uk.yourstore.com for British customers.

  • Multi-stores also occur when they operate separate brands under different domain names. These configurations need individual base URL settings for each store.

Common Base URL Issues in Magento 2 with Solutions

How Base URL Affects Your Entire Store

Hover over each component to see how the base URL impacts different areas

BASE URL

Core Configuration

Frontend Links

Product & category pages

Admin Panel

Backend access URLs

Media URLs

Images & assets

SEO Structure

Search rankings

API Integration

External connections

Cache System

Performance impact

The base URL is the foundation of your Magento store. Every link, image, and connection depends on it being configured correctly. One wrong setting can cascade into multiple issues across your entire system.

1. CSS/JS Loading Issues

  • Browsers fail to load stylesheets and JavaScript files. It is when the base URL changes that don't update all resource paths.

  • Your store appears broken with missing formatting and non-functional elements. The browser console displays 404 errors for CSS and JS files that point to the old domain paths.

  • Static content deployment often retains references to the previous URL configuration. Magento generates static file URLs using cached configuration data. It reflects the old base URL. Users see unstyled pages with broken layouts. It is because the browser cannot locate essential styling files.

2. Image Display Problems

  • Product images disappear from category pages and product listings after URL changes. Magento stores image paths in the database.

  • It uses absolute URLs that reference the previous domain configuration. Customers see broken image placeholders instead of actual product photos throughout the store.

  • Media files fail to load. It is because Magento constructs image URLs using the old base URL settings. Magento Logo images, banner graphics, and CMS content images display as broken links.

  • The media gallery becomes inaccessible in the admin panel. It occurs when image paths don't align with the new URL structure. Product catalogs lose visual appeal and functionality without proper image display.

3. Redirect Loops

Magento 2 change base URL redirect loops issue

  • Servers create infinite redirect cycles. These are when secure and unsecure URL configurations conflict with each other.

  • Browsers get trapped in endless loops trying to access pages. These keep redirecting between HTTP and HTTPS protocols. Customers cannot reach any store pages because their browsers timeout.

  • Magento triggers redirect loops when base URL settings don't match the actual server. Mixed SSL settings cause the store to redirect secure pages to unsecure URLs.

  • It then redirects back to secure URLs. Web servers interpret conflicting URL configurations as instructions. It redirects visitors between different protocol versions.

4. Admin Panel Access Issues

  • Store admins lose access to the Magento backend. This is when admin URLs don't update with the new base URL configuration. Login attempts fail because the admin panel tries to redirect to the old domain. Admins see "Page Not Found" errors or connection timeouts.

  • Cookie domain mismatches prevent successful admin sessions after URL changes. The browser cannot establish proper authentication cookies.

  • It occurs when the admin URL doesn't align with the new domain configuration. Admin users get logged out because session management fails with an incorrect URL.

FAQs

FAQs

Quick answers to common questions about changing Magento 2 base URLs

Will changing my Magento base URL affect my SEO rankings?

SEO

Changing your base URL can hurt your search rankings. Google prefers secure websites, so switching to HTTPS helps your rankings. Set up redirects from your old URLs to new ones. This keeps your search position. Update your sitemap right after the change. Submit it to Google Search Console.

301

Use permanent redirects

24h

Update Search Console

Do I need to clear the cache after changing the base URL in Magento 2?

Cache

Yes, you must clear your cache after changing base URLs. Go to System > Cache Management in your admin panel. Click "Flush Cache Storage" to clear all saved data. This makes your changes work. If your site still displays problems, your server may need to be restarted.

bin/magento cache:flush
bin/magento cache:clean rm -rf var/cache/* var/page_cache/*

How do I update email templates after changing my base URL?

Email

Email templates usually update when you change your base URL. But you should check them to make sure. Go to Marketing > Email Templates to see your custom templates. Look for any links that still point to your old website.

Check these templates: Order confirmation, shipping notifications, password reset emails, and newsletter templates.

How long does it take for base URL changes to take effect?

Time

Your base URL changes take effect after you save and clear the Magento cache. But some things take longer to update. Your browser might remember the old website for a while. Try using a different browser to see if the changes take effect.

DNS propagation: Can take up to 48 hours for domain changes to fully propagate worldwide.

What special considerations apply to extensions after base URL changes?

Extensions

Add-on modules often save your old website address in their settings. Check Stores > Configuration for any extensions that might have your old URL. Payment systems need their settings updated with your new website address. Social media connections need new callback URLs.

Common Extensions to Check:
  • Payment gateways
  • Shipping providers
  • Social login modules
  • Analytics tools

Summary

The Magento 2 change base URL task is carried out by stores for SEO strategies. This article covers two methods for changing the base URL, along with their common issues. Here is a recap:

  • Base URL controls all website links and functionality.

  • Four methods exist: admin panel, database, CLI, and configuration.

  • Clear the cache and test after changing URLs.

  • Common problems include broken CSS, images, and redirects.

  • Backup your store before making any URL changes.

Choose managed Magento hosting to change base URLs and enhance SEO strategies.

Nanda Kishore
Nanda Kishore
Technical Writer

Nanda Kishore is an experienced technical writer with a deep understanding of Magento ecommerce. His clear explanations on technological topics help readers to navigate through the industry.


Get the fastest Magento Hosting! Get Started