Wordpress - Relative or dynamic site url possible?

I usually just avoid the issue entirely every time I create a new wordpress site:

define('WP_HOME', '/');
define('WP_SITEURL', '/');

will cause wordpress to use root-relative urls for everything. Makes site migrations to other domains far easier. Ofc, if you access your site using a folder (eg. "http://<domain>/blog") you could change them to:

define('WP_HOME', '/blog/');
define('WP_SITEURL', '/blog/');

For existing sites, make sure the database and any theme/plugin files are free from absolute urls generate by wordpress using the old WP_HOME and WP_SITEURL values.

EDIT: just to clarify, you add these defines to your wp-config.php.


If anyone out there has a similar situation as me, I found a solution by adding:

/* That's all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
/* THIS IS CUSTOM CODE CREATED AT ZEROFRACTAL TO MAKE SITE ACCESS DYNAMIC */
$currenthost = "http://".$_SERVER['HTTP_HOST'];
$currentpath = preg_replace('@/+$@','',dirname($_SERVER['SCRIPT_NAME']));
$currentpath = preg_replace('/\/wp.+/','',$currentpath);
define('WP_HOME',$currenthost.$currentpath);
define('WP_SITEURL',$currenthost.$currentpath);
define('WP_CONTENT_URL', $currenthost.$currentpath.'/wp-content');
define('WP_PLUGIN_URL', $currenthost.$currentpath.'/wp-content/plugins');
define('DOMAIN_CURRENT_SITE', $currenthost.$currentpath );
@define('ADMIN_COOKIE_PATH', './');

In the wp-config.php I found this solution on the site: http://davidmregister.com/dynamic-wp-siteurl/

Thanks everyone!