Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set WordPress site URL in the config file instead of the database
<?php
// WordPress stores the site URL in the database by default (which I have never
// understood), and it's a pain to have to type out the UPDATE SQL or search in
// phpMyAdmin to change it. This is a simple way to put the URL into
// wp-config.php instead.
// Note that you will still need to update any URLs that appear in the content,
// especially when you copy a database from a development site to production:
// https://gist.github.com/davejamesmiller/a8733a3fbb17e0ff0fb5
//----------------------------------------
// Add these lines to wp-config.php:
define('WP_SITEURL', 'https://www.domain.com');
define('WP_HOME', 'https://www.domain.com');
//----------------------------------------
// Or add appropriate logic so it works on all servers:
if ($_SERVER['HTTP_HOST'] == 'dev.domain.com') {
define('WP_SITEURL', 'http://dev.domain.com');
define('WP_HOME', 'http://dev.domain.com');
} else {
define('WP_SITEURL', 'https://www.domain.com');
define('WP_HOME', 'https://www.domain.com');
}
//----------------------------------------
// Or you can use the current hostname automatically - but note that WordPress
// won't be able to redirect to the canonical domain for you
define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
//----------------------------------------
// Or use .env files like Laravel does (you can also use it for database credentials)
// (Also run "composer require vlucas/phpdotenv")
require_once __DIR__ . '/../vendor/autoload.php';
$dotenv = new Dotenv\Dotenv(__DIR__ . '/..');
$dotenv->load();
define('WP_SITEURL', $_ENV['URL']);
define('WP_HOME', $_ENV['URL']);
@brasilmorar

This comment has been minimized.

Copy link

brasilmorar commented Aug 31, 2015

Thanks very good,

I set up a wordpress site on Amazon, but when I added the Elastic Load Balancing service that define the parameters to force the url enter as "www" does not work anymore. I found your post with the second option: "Or add Appropriate logic so it works on all servers" so everything went back to work.

Thank you so much

@vadikcoma

This comment has been minimized.

Copy link

vadikcoma commented Sep 26, 2016

Hello, how do I enable https and www suffix by default when installing wordpress? Your example works perfect for adding https automatically, however I coulnt figure out how to add www. At the moment when I install wordpress, by default I have following settings: http://example.com. But I want it to be https://www.example.com by default. Is that possible?

Thanks!

@davejamesmiller

This comment has been minimized.

Copy link
Owner Author

davejamesmiller commented Sep 26, 2016

@vadikcoma
First, make sure you specified the www. part in both constants above. If you did, WordPress won't redirect automatically but all the links to subsequent pages should go to the correct URL.

If you want a canonical redirect, there are a few ways (e.g. plugins) but I do it by adding this to .htaccess - it must go before the WordPress rules:

# Always use SSL
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Canonical redirect
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

Since you're using HTTPS, you might also want to enable HSTS - but only if you understand the implications:

Header set Strict-Transport-Security "max-age=31536000" env=HTTPS

(PS "www." is a prefix not a suffix.)

@damiansutton

This comment has been minimized.

Copy link

damiansutton commented Jan 3, 2017

This is fantastic.

But I need to get this working with a multisite setup.

So I have updated the wp-config.php file with

define('WP_ALLOW_MULTISITE', true );

And now, the domain keeps jumping back to the domain defined in the database, rather than using:

define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);

Any ideas on how to make this work for a multisite setup?

@davejamesmiller

This comment has been minimized.

Copy link
Owner Author

davejamesmiller commented Oct 10, 2017

@damiansutton I don't think it can be done for Multisite because the URLs are in the database. I use WP-CLI to search and replace them as needed.

@plm66

This comment has been minimized.

Copy link

plm66 commented Apr 3, 2018

Thanks. You save me a lot of time.

@ghost

This comment has been minimized.

Copy link

ghost commented Mar 21, 2019

Thank you so much for this tip. I had an affiliate website and somehow it hacked. The siteurl in the database was getting changed in every 2-4 hours. I am still debugging the issue but due to this particular tip, I can take a proper sleep.

@mikehchase

This comment has been minimized.

Copy link

mikehchase commented May 18, 2019

This was great. I'm about to practice a WP GitHub workflow from Codeanywhere to MAMP and was wondering how to handle the difference in the wp_options database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.