Can I change part of a url?

by vjboc
16 replies
I have a dynamic website. The urls are affiliate links. Is it possible to change part of the url after the fact? Example: The url is "example.com/blahblahblah/3333333/id3"
Can it be changed to example.com/blahblahblah/5555555/id3

Basically change the 3's to 5's maybe with mod rewrite or something.
The site is asp.net.
Thanks in advance.
#change #part #url
  • Profile picture of the author 26medias
    What you are searching for is called "Url rewriting".
    here is an official article from Microsoft:
    URL Rewriting in ASP.NET
    {{ DiscussionBoard.errors[6837842].message }}
  • Profile picture of the author SteveSRS
    or if you have apache server:
    mod_rewrite Cheat Sheet (V2) - Added Bytes - Brighton Web Development
    mod_rewrite, a beginner's guide (with examples)
    Apache Server Mod Rewrite Tips, Tricks and Forum Modrewrite.com

    It looks harder then it really is! Just play with it on non public site and you will get the hang of it asap

    Edit: oh I missed that TS wrote that his site was in asp.net sorry about that, I'll leave the above info for others who mind find this topic
    {{ DiscussionBoard.errors[6839092].message }}
  • Profile picture of the author KirkMcD
    Are these incoming links or outgoing links?
    Incoming see the ASP Rewriting mentioned above.
    If they are outbound links, you'll need to edit your pages. There are tools that will allow you to search and update multiple files at one time.
    {{ DiscussionBoard.errors[6842450].message }}
    • Profile picture of the author vjboc
      Originally Posted by KirkMcD View Post

      Are these incoming links or outgoing links?
      Incoming see the ASP Rewriting mentioned above.
      If they are outbound links, you'll need to edit your pages. There are tools that will allow you to search and update multiple files at one time.
      These are out going links, they are dynamic. Thousands of links. Basically I am trying to convert one part of the url string. It is the same code on every url. Example example.com/products/3333333/id3.aspx

      Is it possible to just change the 3's to another number, after the fact? If so can someone show me how.
      {{ DiscussionBoard.errors[6846567].message }}
  • Profile picture of the author clickbump
    You can change the URL to just about anything you want. The key is that you will need to set up 301 redirects in order to account for links pointing to the previous URL of the page.
    Signature
    {{ DiscussionBoard.errors[6842803].message }}
  • Profile picture of the author pikachum3
    Sure, you can change anything you want but when change, there will be some changes on you site and it will be not good!
    {{ DiscussionBoard.errors[6846855].message }}
    • Profile picture of the author vjboc
      Originally Posted by pikachum3 View Post

      Sure, you can change anything you want but when change, there will be some changes on you site and it will be not good!
      In what way?
      {{ DiscussionBoard.errors[6848225].message }}
  • Profile picture of the author vjboc
    I really appreciate all the responses, but what I am wanting to do is to change the url to a particular url. They are dynamic links, so I cant do a redirect. What I want to do is change the affiliate id. Example: (domain.com/products/3333333/id3.aspx) to (domain.com/products/3333333/id3.aspx) when clicked.

    The links on the page has this code. When clicked to go to domain.com/products/3333333/id3.aspx it will redirect to domain.com/products/555555/id3.aspx
    I just want the numers in the url 333333 to 555555 changed. Is this possible in asp
    {{ DiscussionBoard.errors[6851159].message }}
    • Profile picture of the author jmartinez
      Ok so you have thousands of links on your site that are dynamically created and that look something like this:

      Code:
      <a href="domain.com/products/3333333/id3.aspx">Link I want to change</a>
      And you want to change that so that it loads another url.

      Code:
      <a href="domain.com/products/5555555/id3.aspx">Link I want to appear on my page</a>
      Is that correct?

      If it is, then you can use javascript to do this. It makes no difference if your pages are dynamic. There are a few ways to get this done, but here is how to do it in concept.

      Drop a javascript into your dynamic template. Your javascript will execute when the page loads. What the script does is it grabs all the links on your page that you want to change, swaps out the original links with the ones you want. It does this on page load. Thus, all your links should be legit.

      Everything is done after the page is generated from the server. So, it doesn't matter if your dynamic script generates the pages and links. Javasciprt will change all links after the page loads when it arrives in the visitors browser.

      Another way would be to cloak the links. Javascript again. Your script will disable the default action of the links on your page, instead of loading a page when a link is clicked, it instead executes your javascript. That script will swap the link and the redirect the page to the url you want it to load.

      If you need any assistance let me know. it shouldn't be too hard to do.
      {{ DiscussionBoard.errors[6851804].message }}
      • Profile picture of the author vjboc
        Originally Posted by jmartinez View Post

        Ok so you have thousands of links on your site that are dynamically created and that look something like this:

        Code:
        <a href="domain.com/products/3333333/id3.aspx">Link I want to change</a>
        And you want to change that so that it loads another url.

        Code:
        <a href="domain.com/products/5555555/id3.aspx">Link I want to appear on my page</a>
        Is that correct?

        If it is, then you can use javascript to do this. It makes no difference if your pages are dynamic. There are a few ways to get this done, but here is how to do it in concept.

        Drop a javascript into your dynamic template. Your javascript will execute when the page loads. What the script does is it grabs all the links on your page that you want to change, swaps out the original links with the ones you want. It does this on page load. Thus, all your links should be legit.

        Everything is done after the page is generated from the server. So, it doesn't matter if your dynamic script generates the pages and links. Javasciprt will change all links after the page loads when it arrives in the visitors browser.

        Another way would be to cloak the links. Javascript again. Your script will disable the default action of the links on your page, instead of loading a page when a link is clicked, it instead executes your javascript. That script will swap the link and the redirect the page to the url you want it to load.

        If you need any assistance let me know. it shouldn't be too hard to do.

        Yes, this is what I am looking for. What do I put in the javascript file to make this work?
        {{ DiscussionBoard.errors[6852409].message }}
  • Profile picture of the author vjboc
    Jmartinez, Just an extra note. There are about 10 of these links on every page. What I am hoping that when the page loads, it changes all the links on the page, to the new url.
    {{ DiscussionBoard.errors[6852457].message }}
    • Profile picture of the author jmartinez
      Originally Posted by vjboc View Post

      Jmartinez, Just an extra note. There are about 10 of these links on every page. What I am hoping that when the page loads, it changes all the links on the page, to the new url.
      Having 10 links per page shouldn't be a problem. You only need to match all ten of them and not any others, and then swap out the old url with the new one.

      If you have jquery included in your page you can use something like this:

      Code:
      $('a[href*="333333"]').each(function() {
      
          var newUrl = $(this).attr('href').replace("333333","555555");
          $(this).attr('href',newUrl);
          
      
      });
      That swaps all links with the 333333 pattern and replaces them with the 555555 pattern. Just replace the 333333 and 555555 in the code with whatever you want to swap. Be sure you don't get them backwards.

      In english, that matches all 'a' tags on your page with the 333333 in the url. It loops through each of them, replaces the 333333 pattern with 555555, loads that into the newUrl variable, then updates the link on your page with it.

      You may do a variation on this depending on what your links look like. Just be sure you don't match links you don't want to change. In other words, if you have links with the 333333 in them that you want to keep, then you will need to be more specific on the pattern you want to match.
      {{ DiscussionBoard.errors[6856242].message }}
      • Profile picture of the author vjboc
        Originally Posted by jmartinez View Post

        Having 10 links per page shouldn't be a problem. You only need to match all ten of them and not any others, and then swap out the old url with the new one.

        If you have jquery included in your page you can use something like this:

        Code:
        $('a[href*="333333"]').each(function() {
        
            var newUrl = $(this).attr('href').replace("333333","555555");
            $(this).attr('href',newUrl);
            
        
        });
        That swaps all links with the 333333 pattern and replaces them with the 555555 pattern. Just replace the 333333 and 555555 in the code with whatever you want to swap. Be sure you don't get them backwards.

        In english, that matches all 'a' tags on your page with the 333333 in the url. It loops through each of them, replaces the 333333 pattern with 555555, loads that into the newUrl variable, then updates the link on your page with it.

        You may do a variation on this depending on what your links look like. Just be sure you don't match links you don't want to change. In other words, if you have links with the 333333 in them that you want to keep, then you will need to be more specific on the pattern you want to match.

        jmartinez, This is awseome, Thank you so much for helping me with this. It works great. This is a nice feature to have for other things as well
        Thanks again.
        {{ DiscussionBoard.errors[6856644].message }}
  • Profile picture of the author lordspace
    Hi,

    If the link is under your domain then you have control. On the other hand, if the link is controller by another site you'll have to look the control panel that you were given access to.
    If you can't find the answer then you should contact the company whose products you'll be promoting.

    ideally, you should have this setup:
    a) yourdomain.com/go/product1 => the user goes to a landing page => on that page you put your affiliate link.

    b) yourdomain.com/go/product1 => which will redirect to your affiliate link.

    You may ask why do you have to redirect twice .... because you'll always be using your link e.g. yourdomain.com/go/product1 because if your affiliate code changes you'll have to update only on location i.e. where yourdomain.com/go/product is pointing to.

    I hope this makes sense to you.
    Signature

    Are you using WordPress? Have you tried qSandbox yet?

    {{ DiscussionBoard.errors[6853298].message }}
  • Profile picture of the author FredBliss
    Regular Expressions. One of the most powerful ways to manipulate/handle text, it is supported by all major programming languages used by web developers and including Javascript it can be used to conveniently alter portions of strings. This one is pretty easy, what you would do is look for the numerical portion and do a replace function on it in whatever programming language you are using.

    The pattern:
    Would handle it

    string.replace(/(\d+)[^\/]/g, "555555"); this would replace *any* numerical with the second string. To be more strict, do

    /(3+)[^\/]/g,"555555");

    Let me break it down

    (\d+) - this is a "capture group" the "\d" means a digit e.g. 0-9. The "+" means find at least 1, or more of them in the string together. the grouping is to be sure it only looks for 1 or more digits 0-9 for a match. Then [^\/] tells it to ignore the "/" at the beginning and end of the numbers. "/g" tells it to look globally in the string, and the first / denotes the beginning of the pattern.

    Test it out on

    RegExr

    HTH!
    {{ DiscussionBoard.errors[6858229].message }}
  • Profile picture of the author whiterock
    You can do mod rewrite for rewrite your url thats the only way by which you can change the url or part of the url
    {{ DiscussionBoard.errors[6865147].message }}

Trending Topics