Cant login to my wp-admin after switching to SSL
When you migrate a WordPress site from HTTP to HTTPS, the most common symptom of misconfiguration is being unable to access the admin dashboard. The browser may appear to refresh the page indefinitely or simply redirect you back to the homepage without preserving your session state. This behavior typically stems from a mismatch between the URL stored in the database and the protocol defined by your server environment variables.
The core issue described involves the login form action pointing to the old HTTP site. In WordPress, the wp-login.php file handles authentication, but where it sends the user after loginâor where it attempts to redirect them during the processâis determined by several dynamic functions. The most critical of these is site_url(). When you inspect the HTML source while logged in via a browser that has cached cookies for the HTTP domain, you might see the form action pointing to http://example.com/wp-admin/admin-ajax.php instead of https://example.com.
This discrepancy occurs because WordPress relies on the $_SERVER['HTTPS'] variable and the WP_HOME or HOME constants to determine the site's protocol. If your server is configured with a load balancer, reverse proxy (like Nginx or Apache), or an SSL termination layer at the CDN level, the incoming request arrives as HTTP on port 80, even though the client sees HTTPS. Consequently, $_SERVER['HTTPS'] evaluates to false, causing WordPress to generate all URLs using HTTP by default.
To resolve this, you must explicitly define the site's protocol in your environment so that WordPress generates absolute URLs for the admin dashboard and assets correctly. The most robust solution is to set the HTTPS constant before WordPress loads its core files. You can do this by adding a line to your wp-config.php file:
define( 'HTTPS', true );
However, simply setting this constant might not be sufficient if you are using a reverse proxy or CDN. In those scenarios, it is often better to define the HOME and SITEURL constants explicitly with the full HTTPS URL. This ensures that all internal redirects, including the admin login form action, use the correct protocol:
define( 'HOME', 'https://example.com' );
define( 'WP_HOME', 'https://example.com' );
define( 'SITEURL', 'https://example.com' );
Another frequent pitfall involves database inconsistencies. If you previously updated your site URLs using the "Force Site URL Change" tool or a plugin, but then switched back to SSL without updating the siteurl and home options in the wp_options table, WordPress will continue to serve HTTP links. You can verify these values by running a SQL query:
SELECT option_name, option_value
FROM wp_options
WHERE option_name IN ('siteurl', 'home');
If the option_value columns contain http://, update them to match your SSL-enabled domain. Be cautious when doing this directly in the database; it is generally safer to use a tool like WP-CLI if available:
wp config set --path=wp-config.php home https://example.com
wp config set --path=wp-config.php siteurl https://example.com
There is also a nuance regarding cookies. If your server has not been properly configured to send the Strict-Transport-Security header or if mixed content warnings are present, browsers may block cookies sent over HTTPS that originated from an HTTP context. This can cause the login session to fail immediately after submission because the browser refuses to store the authentication cookie on the secure domain.
If you are managing multiple sites or using a shared hosting environment where SSL is handled externally, ensure that your .htaccess file (or Nginx config) correctly rewrites all traffic to HTTPS before WordPress processes the request. A common pattern in .htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
By enforcing HTTPS at the Apache level, you ensure that $_SERVER['HTTPS'] is recognized as true when WordPress initializes, preventing the generation of incorrect form actions. This approach avoids the need to rely solely on PHP constants and provides a more secure foundation for your site's authentication flow.
If these steps do not resolve the issue, consider checking if a security plugin or caching layer is rewriting URLs incorrectly. Some caching plugins store redirects based on the original HTTP request; clearing their cache after enabling SSL is often necessary to prevent stale redirect rules from interfering with the new HTTPS configuration.