Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in wp-admin when setting the URL to just a forward slash #147

Open
daveherman71 opened this issue Jun 30, 2021 · 0 comments
Open

Error in wp-admin when setting the URL to just a forward slash #147

daveherman71 opened this issue Jun 30, 2021 · 0 comments

Comments

@daveherman71
Copy link

I have some pages on my site that I need to redirect to the home page (due to legacy SEO).

However redirecting to "/" causes an error in wp-admin.

A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 1

Filename: classes/plugin.php

Line Number: 799

This is the function where the error occurs:

	public static function absolute_url( $url ) {
		// Convert server- and protocol-relative URLs to absolute URLs.
		if ( '/' === $url[0] ) {
			// Protocol-relative.
			if ( '/' === $url[1] ) {
				$url = set_url_scheme( 'http:' . $url );
			} else {
				// Host-relative.
				$url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $url );
			}
		}

		if ( 'mailto' !== parse_url( $url, PHP_URL_SCHEME ) ) {
			$url = str_replace( '@', '%40', $url );
		}

		return $url;
	}

My proposed fix is to first check the length of the string before checking if it is protocol-relative, as follows:

	public static function absolute_url( $url ) {
		// Convert server- and protocol-relative URLs to absolute URLs.
		if ( '/' === $url[0] ) {
			// Protocol-relative.
			if ( strlen($url) > 1 && '/' === $url[1] ) {
				$url = set_url_scheme( 'http:' . $url );
			} else {
				// Host-relative.
				$url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $url );
			}
		}

		if ( 'mailto' !== parse_url( $url, PHP_URL_SCHEME ) ) {
			$url = str_replace( '@', '%40', $url );
		}

		return $url;
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant