如何使用PHP将主机名更改为其他主机名

I have a simple HTML code:

<html>
<head>
<title>Title</title>
</head>
<body>
<p>Test Text<p>
<a href="/gallery/images">Link</a>
</body>
</html>

Of course on local host the Link (/gallery/images) will be:

localhost/gallery/images

My question is; is there a way to change the (localhost) directly when the page is loading and the final html code may be:

<html>
<head>
<title>Title</title>
</head>
<body>
<p>Test Text<p>
<a href="http://newhostname.com/gallery/images">Link</a>
</body>
</html>

= the html of the link becomes:

<a href="http://newhostname.com/gallery/images">Link</a>

Note: if it could done using JavaScript (I think) please tell me how.

I don't know that I completely understand the question, but the HTML base tag seems to be what you're asking for. It allows you to:

Specify a default URL and a default target for all links on a page

In your case, put this in your <head>:

<base href="http://newhostname.com/">

I'm trying to have a better understanding of your question.

If you keep the address as "/gallery/images" it will work first fine when you are migrating from the localhost to a web server. (as long as the dir /gallery/images exists of course)

Remove the hostname completely and just use an absolute path such as /gallery/images/

Also, you could try

$_SERVER['HTTP_HOST']
$_SERVER['SERVER_NAME']