Skip navigation
This discussion is locked
Currently Being Moderated

Can a site tell the browser to consider all (root-relative) links as belonging to another domain?

Jun 12, 2013 2:32 PM

One of my websites fetches its images on another server (and domain). Therefore, instead of root-relative paths, I have to use full http:// urls.

 

This isn't a big deal, except that the domain where these images are stored also, occasionally, changes. Which forces me to search/replace all http:// links, so they point to the new domain.

 

I was just thinking that a cleaner solution (which may - hopefully - already exist) might be to insert one line at the top of the HTML file telling the browser to consider all root-relative links as belonging to another domain.

 

Something like :

 

<meta domain="alternatesite.com" />

<img src="/images/image.jpg" />

 

Where only one line would need to be modified, and the code would be read like it's located on alternatesite.com, even though it's on site.com.

 

Is this already do-able and if so, what's the proper syntax?

 

Thanks!

 
Replies
  • Currently Being Moderated
    Jun 12, 2013 2:41 PM   in reply to jyeager11

    Do you have any skill with server scripting? That would certainly be one way to do it.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 12, 2013 6:32 PM   in reply to jyeager11

    With server side it's easy. Here's how you can do it w/ php.

     

    Step 1. Change .html extensions on your site to .php so that your pages can process php.

     

    Step 2. Create a new page and name it domain.php

     

    Step 3. In domain.php define your root domain by setting a value for a variable like this:

     

    <?php $domain = "third-party-domain.com"; ?>

     

    Step 4. include the $domain variable in domain.php on every one of your pages by adding this line of code to very top of all of your pages:

     

    <?php include('domain.php'); ?>

     

    Step 5. on all of your image tags that are fetching images form thrid-party-domain.com add this line of code to echo the variable for the $domain like this:

     

    <img src="http://<?php echo $domain; ?>/path/to/image.jpg" />

     

    Now all you have to do is change $domain's variable value in domain.php whenever the domain that is hosting the images changes and all of the image sources on your site will be updated to include the new domain URL in the image path.

     

    best,

    Shocker

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 12, 2013 8:10 PM   in reply to jyeager11

    I just need to make the browser think it's on a different domain.

    If it's not doable, then it's not doable.

     

    One would certainly hope that is not doable for obvious security reasons. .htaccess can help with YOUR domain (rewriting domain.com to sub.domain.com for instance), but not to spoof your domain for some other domain. Just think... then you could write cookie or session variable for that other domain and cause a whole lot of hurt for other domain's security, hence why it's not doable.

     

    You can't tell browser, "hey, I'm here" when you're not actually there. However, you can tell browser on a global scale, "hey, GO here" which is what I tried to explain in the previous post.

     

    best,

    Shocker

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 12, 2013 10:19 PM   in reply to the_shocker

    I'll add two cents worth to that... If you don't own the domain or content you're pointing to and/or don't have permission to take their bandwidth, you can add legal problems to the security issues.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 13, 2013 10:27 AM   in reply to jyeager11

    If both websites are on the same server, (they can be in different cPanel accounts) and you have the ability to edit the http.conf file, you can create an alias. You can also do this if you have WHM (web host manager) access. This will do exactly what you are asking -- it will allow you to use relative links for a location that is elsewhere.

     

    Here is an example:

    Alias /color_shared /home/mywebsite/public_html/color

     

    The above allows any website on the server to use "/color_shared" to access the "color" directory in the "mywebsite" root.

     

    An even simpler method is to point both domains to different directories on the same root, then they can share the files are are above their respective roots using relative paths. Actually, they can even share files that are below their respective roots.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 13, 2013 11:47 AM   in reply to jyeager11

    As the_shocker mentioned, doing it with .htaccess could open up security issues. It's pure hypothesis, but If someone were to hack this one site and get to the .htaccess file, all of your sites could then be compromised. PHP would be a better solution from that standpoint, but interlinking several sites always presents the risk of hacking of all of them as opposed to keeping them "dedicated".

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 13, 2013 12:45 PM   in reply to jyeager11

    It's not do-able via htaccess.

     

    By the way, my method 2 is incorrect. Won't work that way. The only way is via alias.Otherwise, the method the Shocker described.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 13, 2013 3:17 PM   in reply to jyeager11

    Dude, how many times do people need to say it's not doable before you realize it's not doable?!? Do you think if you keep asking the same question that somehow you will get the answer you want? Are you trying to shock the_shocker?!? I thought I explained it very clear in my last post. Again, the answer is: it's not doable.

     

    Your last post complains about this:

    Every method I've read in this thread - including shocker's - involves modifying all 5,000 links individually.

     

    and yet your original post explains this:

     

    One of my websites fetches its images on another server (and domain). Therefore, instead of root-relative paths, I have to use full http:// urls.

     

    This isn't a big deal, except that the domain where these images are stored also, occasionally, changes. Which forces me to search/replace all http:// links, so they point to the new domain.

     

    You're saying in OP that you're currently using full http:// urls and that you currently search/replace http:// links so they point to new domain. Why is it so difficult for you to do this ONE more time and find current http:// urls and replace with http://<?php echo $domain; ?>? You could of already had the first method implemented that I instructed you to do between then and now.

     

    Let's just say for a moment, for the sake of discussion, that is was doable. To be very clear, it's not doable. This is for sake of discussion. If it was doable and you could just use relative paths you'd still have to go through and remove all the absolute domains in your links, so there's really no differene doing that than there is to replace with dynamic domain variable. Either way you'd still have to go through and modify all your links again according to the current method you explained in OP.

     

    Captain obvious tells me that you should of thought about this before you decided to setup 5,000 links this way and then go on to forum trying to get pity from others by saying you have to modify 5,000 links. Nobody here cares that you have to update 5,000 links. Scale makes no difference in regards to your request being doable or not.

     

     

    best,

    Shocker

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 13, 2013 5:52 PM   in reply to jyeager11

    The http's mentioned in the OP are the clumsy way I am currently dealing with the issue with my limited knowledge of web development. I also mention in that very same OP that I'm looking for a cleaner alternative to this; specifically asking about the possibility of modifying just one line somewhere telling the browser to consider all root-relative links as belonging to another domain. But you conveniently forgot to mention this part when throwing the OP back in my face, didn't you?

     

    Unfortunately, confusion has defeated you again. I conveniently remembered to mention it by reminding you that you'd still have to go through and edit all links regardless. You just failed to comprehend. Here it is again to refresh your memory.

    the_shocker wrote:


    Let's just say for a moment, for the sake of discussion, that is was doable. To be very clear, it's not doable. This is for sake of discussion. If it was doable and you could just use relative paths you'd still have to go through and remove all the absolute domains in your links, so there's really no differene doing that than there is to replace with dynamic domain variable. Either way you'd still have to go through and modify all your links again according to the current method you explained in OP.

     

    I would love to tell you to 'F' yourself for being so ignroant. Fortunately I am more mature than that, friend. Eventually you'll realize that there is no other way to accomplish the desired result other than the method I described earlier; and others concurred. The sooner you realize that the sooner I can start being accused of cyberbullying some other non-expert out there.

     

    best,

    Shocker

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 13, 2013 5:44 PM   in reply to jyeager11

    Apart from what has already been said above, have you thought of trying "Search & Replace" in DW assuming all your files are readily available on your system under a properly/correctly defined site?

     

    In DW you need to define a site to get maximum benefit out of it so it is worth a try.

     

    G/L

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 13, 2013 6:39 PM   in reply to jyeager11

    I just looked into an alternate method to accomplish this task. Rest assured that I was able to accomplish the result you desire with only two lines of code. It only took a few minutes to setup and I have absolutely no intention of explaining to you how it was accomplished. Perhaps I'm even more of an a****** than you thought now? FYI: it was not accomplished using .htaccess and I did not need to edit any links. I simply added two lines of code and all relative paths served images from alternatedomain.com

     

    Too bad you're out of cherries or else you could ask me w/ a cherry on top how it was accomplished (and I would of been happy to assist you too because I really like cherries). Maybe someone else that cares enough to assist you after your little immature temper-tantrum can figure out what I did and hold your hand to walk you through it. Don't hold your breath though. On second thought... hold your breath forever.

     

    best,

    Shocker

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 13, 2013 7:38 PM   in reply to the_shocker

    Shocker, jyeager11

     

    I am sure that when you look at this thread tomorrow after your tempers have cooled down, there will be a better resolution to this thread. Speaking for myself, I find discussions especially useful when human emotions are left out of it, especially technical discussions. And I am sure, our many users that visit this forum feel the same way too.

     

    Shocker: It would be wonderful if you could post your answer here. There will be other users visiting this thread at a later point in time, and they will find it immensely helpful.

     

    Good night folks, and let the peace of the night work its wonders on you.

     

    And we are already at the weekend.


    Cheers,

    Preran with the white flag (and a dove on it)

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 14, 2013 8:37 AM   in reply to jyeager11

    <!DOCTYPE html>

    <html>

    <head>

    <title>jQuery dynamic domain replacement in image src compliments of the_shocker</title>

    <script src="http://code.jquery.com/jquery-latest.min.js" /></script>

    <script>

    $( document ).ready(function() {

              // find <img> elements that contain www.yourwebsite.com in the src attribute

              $('img[src*="www.yourwebsite.com"]').attr('src', function(x,src) {

                        // return a new version of the image src

                        // by using the replace method in jQuery

                        // to replace 'yourwebsite' with 'otherwebsite'

                        return src.replace('yourwebsite', 'otherwebsite');

              });

    });

    </script>

    </head>

    <body>

              <img src="http://www.yourwebsite.com/images/pets/dog.jpg" />

              <img src="http://www.yourwebsite.com/images/pets/cat.jpg" /> 

              <img src="http://www.yourwebsite.com/images/toys/hamster.jpg" />

              <img src="http://www.yourwebsite.com/images/toys/donkey.jpg" />

    </body>

    </html>

     

    best,

    Shocker

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 14, 2013 1:32 PM   in reply to jyeager11

    Now that we have come full complete cycle, I am locking this thread in everyone's best interests.

     

    Have a wonderful weekend, everyone!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points