Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. Url rewriting rule

Url rewriting rule

Scheduled Pinned Locked Moved Regular Expressions
comregextutorial
3 Posts 3 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gaurav Goel Team Lead
    wrote on last edited by
    #1

    Hello there, I need some clever regular expression writing in IIS7 to redirect traffic from one site to another, the pages are similar in structure but information needs re-writing. Here's an example: http://www.mysite.com/manufacturers/Name\_of\_Manufacturer\_6828/Name\_OF\_Product\_43146.htm needs to be structured as the following http://www.newsite.co.in/free-msds-download/name-of-manufacturer/name-of-product/ Here's what needs changing: 1). Only Alpha, Numeric and hyphen's allowed. 2). Change '_' to '-'. 3). Remove the '_NUMBER' directly after the manufacturer name and before the '/'. 4). Remove the '_NUMBER' directly after the product name and before the '.htm'. 5). Remove the '.htm' and replace with a '/'. It needs writing into the code below please as a permanent 301 redirect:

    S A 2 Replies Last reply
    0
    • G Gaurav Goel Team Lead

      Hello there, I need some clever regular expression writing in IIS7 to redirect traffic from one site to another, the pages are similar in structure but information needs re-writing. Here's an example: http://www.mysite.com/manufacturers/Name\_of\_Manufacturer\_6828/Name\_OF\_Product\_43146.htm needs to be structured as the following http://www.newsite.co.in/free-msds-download/name-of-manufacturer/name-of-product/ Here's what needs changing: 1). Only Alpha, Numeric and hyphen's allowed. 2). Change '_' to '-'. 3). Remove the '_NUMBER' directly after the manufacturer name and before the '/'. 4). Remove the '_NUMBER' directly after the product name and before the '.htm'. 5). Remove the '.htm' and replace with a '/'. It needs writing into the code below please as a permanent 301 redirect:

      S Offline
      S Offline
      Shahriar Iqbal Chowdhury Galib
      wrote on last edited by
      #2

      Hi, You can use the JavaScript below,

      var str="http://www.mysite.com/manufacturers/Name\_of\_Manufacturer\_6828/Name\_OF\_Product\_43146.htm";
      str=str.replace("http://www.mysite.com/manufacturers/", "");
      var key=str.split("/");
      var manufacturer=key[0];
      var product=key[1];
      var to_index=manufacturer.lastIndexOf("_", manufacturer.length);
      manufacturer=manufacturer.substring(0, to_index);
      manufacturer=manufacturer.replace(/_/gi, "-");

      product=product.replace(/.htm/gi, "");
      to_index=product.lastIndexOf("_", product.length);
      product=product.substring(0, to_index);
      product=product.replace(/_/gi, "-");
      var cleanUrl="http://www.newsite.co.in/free-msds-download/"+manufacturer+"/"+product+"/";
      document.write(cleanUrl);

      variable cleanUrl holds the url you are looking for.Hope this will help.

      1 Reply Last reply
      0
      • G Gaurav Goel Team Lead

        Hello there, I need some clever regular expression writing in IIS7 to redirect traffic from one site to another, the pages are similar in structure but information needs re-writing. Here's an example: http://www.mysite.com/manufacturers/Name\_of\_Manufacturer\_6828/Name\_OF\_Product\_43146.htm needs to be structured as the following http://www.newsite.co.in/free-msds-download/name-of-manufacturer/name-of-product/ Here's what needs changing: 1). Only Alpha, Numeric and hyphen's allowed. 2). Change '_' to '-'. 3). Remove the '_NUMBER' directly after the manufacturer name and before the '/'. 4). Remove the '_NUMBER' directly after the product name and before the '.htm'. 5). Remove the '.htm' and replace with a '/'. It needs writing into the code below please as a permanent 301 redirect:

        A Offline
        A Offline
        AspDotNetDev
        wrote on last edited by
        #3

        I am going to make a few assumptions. Since you said this is in IIS, you probably can't create .Net or JavaScript code to handle the redirect (i.e., all redirects must be done using some regexes in a configuration file). Also, I assume the number of underscores in the "mysite.com" URL is variable. I don't think you can do this with one redirect, but you can create several. For example, you can start off with these (create more to handle more underscores):

        ^http://www\.mysite\.com/manufacturers/(?<MAN>[a-z0-9]+)_[0-9]+/(?<PRO>[a-z9-9]+)_[0-9]+\.htm$
        http://www\.newsite\.co\.in/free-msds-download/${MAN}/${PRO}/

        ^http://www\.mysite\.com/manufacturers/(?<MAN1>[a-z0-9]+)_(?<MAN2>[a-z0-9]+)_[0-9]+/(?<PRO>[a-z0-9]+)_[0-9]+\.htm$
        http://www\.newsite\.co\.in/free-msds-download/${MAN1}-${MAN2}/${PRO}/

        ^http://www\.mysite\.com/manufacturers/(?<MAN>[a-z0-9]+)_[0-9]+/(?<PRO1>[a-z9-9]+)_(?<PRO2>[a-z9-9]+)_[0-9]+\.htm$
        http://www\.newsite\.co\.in/free-msds-download/${MAN}/${PRO1}-${PRO2}/

        ^http://www\.mysite\.com/manufacturers/(?<MAN1>[a-z0-9]+)_(?<MAN1>[a-z0-9]+)_[0-9]+/(?<PRO1>[a-z9-9]+)_(?<PRO2>[a-z9-9]+)_[0-9]+\.htm$
        http://www\.newsite\.co\.in/free-msds-download/${MAN1}-${MAN2}/${PRO1}-${PRO2}/

        That is simple enough, but that could end up being a lot of rules if you have a large range of underscores (e.g., "the_name_of_some_long_product" and "the_name_of_some_long_manufacturer"). Alternatively, you can redirect all of the "mysite" URL's that match the pattern you want to a single URL on "newsite" that takes the original URL as a query string. The page on newsite can then parse the query string and perform a redirect using C# or VB.Net code, which gives you more flexibility than a configuration file. Note: the above regexes are untested and may contain mistakes, but you get the idea.

        Somebody in an online forum wrote:

        INTJs never really joke. They make a point. The joke is just a gift wrapper.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups