How to make relative URLs work in Mamp and Xampp for WordPress

Posted on: June 26th, 2023
By: Tadeo Martinez

Add this function to your functions.php

function redirect_relative_urls() {
	if (is_admin()) {
	  return;
	}
  
	ob_start('rewrite_relative_urls');
  }
  
function rewrite_relative_urls($content) {
$base_url = home_url('/');

// Regex pattern to match relative URLs
$pattern = '/(href|src)=["\'](?!http|\/\/)([^"\']+)[\'"]/i';

// Replace relative URLs with absolute URLs
$content = preg_replace_callback($pattern, function ($matches) use ($base_url) {
	$relative_url = $matches[2];
	$absolute_url = $base_url . ltrim($relative_url, '/');
	return $matches[1] . '="' . $absolute_url . '"';
}, $content);

return $content;
}

add_action('template_redirect', 'redirect_relative_urls');

Have any questions or comments? Write them below!


Leave a Reply

Your email address will not be published. Required fields are marked *