Title: WP_Http::make_absolute_url
Published: April 25, 2014
Last modified: April 28, 2025

---

# WP_Http::make_absolute_url( string $maybe_relative_path, string $url ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_http/make_absolute_url/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wp_http/make_absolute_url/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_http/make_absolute_url/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_http/make_absolute_url/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_http/make_absolute_url/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_http/make_absolute_url/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_http/make_absolute_url/?output_format=md#wp--skip-link--target)

Converts a relative URL to an absolute URL relative to a given URL.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_http/make_absolute_url/?output_format=md#description)󠁿

If an Absolute URL is provided, no processing of that URL is done.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_http/make_absolute_url/?output_format=md#parameters)󠁿

 `$maybe_relative_path`stringrequired

The URL which might be relative.

`$url`stringrequired

The URL which $maybe_relative_path is relative to.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_http/make_absolute_url/?output_format=md#return)󠁿

 string An Absolute URL, in a failure condition where the URL cannot be parsed, 
the relative URL will be returned.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_http/make_absolute_url/?output_format=md#source)󠁿

    ```php
    public static function make_absolute_url( $maybe_relative_path, $url ) {
    	if ( empty( $url ) ) {
    		return $maybe_relative_path;
    	}

    	$url_parts = wp_parse_url( $url );
    	if ( ! $url_parts ) {
    		return $maybe_relative_path;
    	}

    	$relative_url_parts = wp_parse_url( $maybe_relative_path );
    	if ( ! $relative_url_parts ) {
    		return $maybe_relative_path;
    	}

    	// Check for a scheme on the 'relative' URL.
    	if ( ! empty( $relative_url_parts['scheme'] ) ) {
    		return $maybe_relative_path;
    	}

    	$absolute_path = $url_parts['scheme'] . '://';

    	// Schemeless URLs will make it this far, so we check for a host in the relative URL
    	// and convert it to a protocol-URL.
    	if ( isset( $relative_url_parts['host'] ) ) {
    		$absolute_path .= $relative_url_parts['host'];
    		if ( isset( $relative_url_parts['port'] ) ) {
    			$absolute_path .= ':' . $relative_url_parts['port'];
    		}
    	} else {
    		$absolute_path .= $url_parts['host'];
    		if ( isset( $url_parts['port'] ) ) {
    			$absolute_path .= ':' . $url_parts['port'];
    		}
    	}

    	// Start off with the absolute URL path.
    	$path = ! empty( $url_parts['path'] ) ? $url_parts['path'] : '/';

    	// If it's a root-relative path, then great.
    	if ( ! empty( $relative_url_parts['path'] ) && '/' === $relative_url_parts['path'][0] ) {
    		$path = $relative_url_parts['path'];

    		// Else it's a relative path.
    	} elseif ( ! empty( $relative_url_parts['path'] ) ) {
    		// Strip off any file components from the absolute path.
    		$path = substr( $path, 0, strrpos( $path, '/' ) + 1 );

    		// Build the new path.
    		$path .= $relative_url_parts['path'];

    		// Strip all /path/../ out of the path.
    		while ( strpos( $path, '../' ) > 1 ) {
    			$path = preg_replace( '![^/]+/\.\./!', '', $path );
    		}

    		// Strip any final leading ../ from the path.
    		$path = preg_replace( '!^/(\.\./)+!', '', $path );
    	}

    	// Add the query string.
    	if ( ! empty( $relative_url_parts['query'] ) ) {
    		$path .= '?' . $relative_url_parts['query'];
    	}

    	// Add the fragment.
    	if ( ! empty( $relative_url_parts['fragment'] ) ) {
    		$path .= '#' . $relative_url_parts['fragment'];
    	}

    	return $absolute_path . '/' . ltrim( $path, '/' );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-http.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-http.php#L972)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-http.php#L972-L1043)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_http/make_absolute_url/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_parse_url()](https://developer.wordpress.org/reference/functions/wp_parse_url/)`wp-includes/http.php` |

A wrapper for PHP’s parse_url() function that handles consistency in the return values across PHP versions.

  |

| Used by | Description | 
| [WP_REST_URL_Details_Controller::get_icon()](https://developer.wordpress.org/reference/classes/wp_rest_url_details_controller/get_icon/)`wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php` |

Parses the site icon from the provided HTML.

  | 
| [WP_REST_URL_Details_Controller::get_image()](https://developer.wordpress.org/reference/classes/wp_rest_url_details_controller/get_image/)`wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php` |

Parses the Open Graph (OG) Image from the provided HTML.

  | 
| [_links_add_base()](https://developer.wordpress.org/reference/functions/_links_add_base/)`wp-includes/formatting.php` |

Callback to add a base URL to relative links in passed content.

  | 
| [WP_Http::handle_redirects()](https://developer.wordpress.org/reference/classes/wp_http/handle_redirects/)`wp-includes/class-wp-http.php` |

Handles an HTTP redirect and follows it if appropriate.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_http/make_absolute_url/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.4.0](https://developer.wordpress.org/reference/since/3.4.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_http%2Fmake_absolute_url%2F)
before being able to contribute a note or feedback.