Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Set WordPress site URL in the config file instead of the database
// 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:
// Add these lines to wp-config.php:
define('WP_SITEURL', '');
define('WP_HOME', '');
// Or add appropriate logic so it works on all servers:
if ($_SERVER['HTTP_HOST'] == '') {
define('WP_SITEURL', '');
define('WP_HOME', '');
} else {
define('WP_SITEURL', '');
define('WP_HOME', '');
// 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__ . '/..');
define('WP_SITEURL', $_ENV['URL']);
define('WP_HOME', $_ENV['URL']);

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

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: But I want it to be by default. Is that possible?



davejamesmiller commented Sep 26, 2016

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 ^(.*)$$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.)

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 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment