0

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Nadine Ah
  • 47
  • 1
  • 1
  • 7
  • The code you listed should work just fine - Can you list a scenario in which you want to use that link so we have a better understanding of your question? – ahinkle Jul 30 '16 at 00:48
  • I'm having a hard time wrapping my head around this question. Will you be hosting your own site from your own PC or are you wanting to do this from a hosted site? – Funk Forty Niner Jul 30 '16 at 00:48
  • I don't use any database. I need only to set a desired host (website) so when I run the webpage the desired website will be become in the place of my original website where I upload the webpage. It's something like when you save a webpage using (Ctrl + S), the webpage will saved and all the links (example: /directory/img.png) will become ( http://savedwebsite.com/directory/img.png) I need only to set the host I want to be placed when the page is finally loaded. – Nadine Ah Jul 30 '16 at 00:52
  • @Fred-ii. It's the same, I use WAMP Server as a local host. But exactly yes I will use a hsoting and a domain name. – Nadine Ah Jul 30 '16 at 00:54
  • you can use `$_SERVER['HTTP_HOST']`. – Funk Forty Niner Jul 30 '16 at 00:55
  • you need something like this `Link` – Funk Forty Niner Jul 30 '16 at 01:02
  • ok, seeing that comment in that answer below, you're probably looking for a mod rewrite here or a redirect. – Funk Forty Niner Jul 30 '16 at 01:03
  • @Fred-ii , thank you for reply. But I didn't get exactly the idea :/ How can I use $_SERVER['HTTP_HOST'] ? – Nadine Ah Jul 30 '16 at 01:04
  • if your site is `example.com` for an example, then `Link` will output as `http://example.com/gallery/images` but like I said in another comment and seeing another of yours, might be looking for a mod rewrite or redirection. You wrote 2 different domain names in that comment below. The same on your pc would output `http://localhost/.......` etc. if that's what the question is about. – Funk Forty Niner Jul 30 '16 at 01:05
  • @Fred-ii- , Yes as I say in the comment I want to replace the (example.com) automatically to (newexample.com) -> /The new website I want / how can I use rewrite and redirection to achieve my goal? – Nadine Ah Jul 30 '16 at 01:10
  • the thing is here, why are there 2 different domain names? do you own 2 different domain names and would like to do a redirection if one domain name is called and redirect it to the second one? I'm still having trouble understanding the question fully here. If that is the case, then see this Q&A http://stackoverflow.com/q/18052745/ – Funk Forty Niner Jul 30 '16 at 01:13
  • :/ I don't know WwhyI can't explain the idea. I have a website lets call it website-1 (as the html code I post) and it includes images and links ( for example href="/gallery/images/img.png" ) I want to put a copy of the website homepage in the other site but I want all links and images to still loaded from website-1 so the website-2 will show the images and links. So even in the website-2 the link will works and the images will appear in the homepage. I need this thing to be made automatically and not just edit the whole file manually. ( Like saving the webpage using Ctrl + S) – Nadine Ah Jul 30 '16 at 01:21
  • Did you not try what I wrote already `Link` - I revisited the question to see if there was any new activity, so you'll need to reping me if you see the need to. Other than that, I'm out of ideas here. That's the best I can give you here. @NadineAh the `http://` will automatically populate whatever host it's on. Do try that. I'm under the impression you haven't and looking for a 100% guarantee that it will work without testing it first. – Funk Forty Niner Jul 30 '16 at 02:27
  • You might want to load the saved HTML and then parse it with a DOM parser to replace all relative URLs from href, link, src etc. with absolute URLs using your new domain. – Drakes Jul 30 '16 at 03:27

3 Answers3

0

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)

ahinkle
  • 2,117
  • 3
  • 29
  • 58
  • Thank you for reply. I know that already, but all what I want is to change the actual host to another when the page is fully loaded. For exapmle if the link is : website.com/gallery/images It should becomes: newwebsite.com/gallery/images – Nadine Ah Jul 30 '16 at 00:56
0

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/">
Andy
  • 11,215
  • 5
  • 31
  • 33
0

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

Also, you could try

$_SERVER['HTTP_HOST']
$_SERVER['SERVER_NAME'] 
drooh
  • 578
  • 4
  • 18
  • 46