Create a gist now

Instantly share code, notes, and snippets.

WordPress: 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', 'http://www.domain.com');
define('WP_HOME', 'http://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', 'http://www.domain.com');
define('WP_HOME', 'http://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', 'http://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);
@brasilmorar

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

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
Owner

@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 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?

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