If you’ve recently cloned a Magento 2 website to a staging environment or temporary development URL and suddenly can’t log into Magento admin, you may see this browser console error:

Cookie “admin” has been rejected for invalid domain.

This is a very common Magento 2 issue after:

  • migrating a website
  • creating a staging environment
  • changing domains
  • using temporary Cloudways URLs
  • moving between live and development servers

The problem is usually caused by Magento cookie configuration or incorrect base URLs still pointing to the live website domain.

In this guide, we’ll walk through how to fix the Magento 2 admin cookie issue using command line only, including updating reCAPTCHA domains for staging websites.

Magento staging cookie error

Why This Happens in Magento 2

Magento stores cookie sessions against a specific domain.

For example:

Live website:
www.example.com

Staging website:
magento-12345.cloudwaysapps.com

If Magento is still configured to use:

.cookie_domain = .example.com

the browser will reject the admin session cookie on the staging domain.

This prevents Magento admin login sessions from working correctly.


Common Symptoms

You may experience:

  • Magento admin login redirect loop
  • login form refreshing repeatedly
  • blank admin dashboard
  • invalid form key errors
  • “Cookie admin has been rejected for invalid domain”
  • inability to stay logged into Magento admin

Magento 2 CLI Commands to Fix Admin Cookie Domain Issues

If you cannot access Magento admin, the fastest solution is via SSH and Magento CLI.

Enable Maintenance Mode

php bin/magento maintenance:enable

Allow Your IP Address

php bin/magento maintenance:allow-ips YOUR.IP.ADDRESS


Update Magento Base URLs

Replace with your staging domain:

php bin/magento config:set web/unsecure/base_url "https://your-staging-domain.com/"

php bin/magento config:set web/secure/base_url "https://your-staging-domain.com/"

Example:

php bin/magento config:set web/unsecure/base_url "https://magento-12345.cloudwaysapps.com/"

php bin/magento config:set web/secure/base_url "https://magento-12345.cloudwaysapps.com/"


Clear Magento Cookie Domain

This is the critical fix.

php bin/magento config:set web/cookie/cookie_domain ""

Also reset cookie path:

php bin/magento config:set web/cookie/cookie_path "/"


Ensure HTTPS Is Enabled

php bin/magento config:set web/secure/use_in_frontend 1

php bin/magento config:set web/secure/use_in_adminhtml 1


Flush Magento Cache

php bin/magento cache:flush


Disable Maintenance Mode

php bin/magento maintenance:disable


Clear Browser Cookies

Before testing Magento admin login again:

  • clear cookies for both domains
  • or use a private/incognito browser window

Old session cookies can continue causing problems even after Magento configuration has been fixed.

Magento web errors

Updating Google reCAPTCHA Allowed Domains for Magento Staging Sites

Another common issue on Magento staging environments is Google reCAPTCHA failing after changing domain.

You may see:

  • reCAPTCHA validation errors
  • “Invalid domain for site key”
  • checkout or login forms failing
  • admin login issues

How to Update Allowed Domains

  1. Login to the Google reCAPTCHA admin console
  2. Select your Magento website key
  3. Add your staging or temporary domain

Example:

magento-12345.cloudwaysapps.com

  1. Save changes

Google reCAPTCHA will then allow requests from your staging environment.


Magento 2 Staging Environment Best Practices

When creating Magento staging websites:

Always Update:

  • Base URLs
  • Cookie domain
  • Secure URLs
  • reCAPTCHA domains
  • payment gateway callbacks
  • CDN configuration
  • Cloudflare settings

Also Recommended:

  • disable indexing
  • block search engines via robots.txt
  • disable transactional emails
  • use separate payment sandbox credentials

Magento 2 Maintenance and Support Services

At Measured Designs, we provide:

Based in the UK (Leigh-on-Sea, Essex), we work with busy ecommerce businesses requiring reliable Magento 2 support, troubleshooting and ongoing maintenance.


The Magento 2 “Cookie admin has been rejected for invalid domain” error is usually caused by:

  • incorrect cookie domain settings
  • outdated base URLs
  • staging environment misconfiguration

Fortunately, the issue can usually be fixed quickly via Magento CLI without needing admin access.

If you regularly manage Magento staging environments, keeping a standard deployment and staging checklist can help avoid login and session issues in future.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.