Redirect URL, keep and add to string...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stuart Hunter
    New Member
    • Jan 2011
    • 13

    #1

    Redirect URL, keep and add to string...

    Hi folks, really hoping someone can help me here...

    I'm trying to direct a load of old web pages to one single page that then populates a registration form as necessary. The catch is that it populates the form from a unique URL based argument / string.

    Annoyingly all the pages are missing a product name, so I am looking at finding a way to add this name to the URL, from the redirection page.

    So my question is... is it possible to use Apache so that when a user visits the old page, they are redirected to a new page, that retains the entire previous unique URL and also adds :PRODUCTNAME to the end?
  • Sudaraka
    New Member
    • Jan 2011
    • 55

    #2
    Depends on where you have the PRODUCTNAME in the old page, if it's a part of the old url, you can just pass the old url to the new page as a parameter in the query string parse it on the new page.

    redirect using mod_rewrite using something like this:
    Code:
    RewriteRule (old_page.html) new_page.php?redirected_from=$1 [QSA]

    Comment

    • Stuart Hunter
      New Member
      • Jan 2011
      • 13

      #3
      Thank you for your quick response Sudaraka,

      There currently is no product information on the old page. Instead of revising a lot of old files I thought it would be best to redirect them using .htaccess to a new single page.

      This page would then call the relevant information from the new url and add the additional information to the form as needed, in this case a product name.

      The query so is as below and it would be great to add ':product_name' to the end of it.

      The catch here is that the code generated is different everytime, we cannot copy the entire string, but maybe the htm files name would be sufficient?

      e.g.

      old urls=

      "http://www.test.com/product1.htm?01 234:user_name_o ne"
      "http://www.test.com/product2.htm?43 210:user_name_t wo"

      new urls=
      "http://www.test.com/product1.htm?01 234:user_name_o ne:product_name "
      "http://www.test.com/product1.htm?01 234:user_name_t wo:product_name "
      Last edited by Stuart Hunter; Jan 11 '11, 12:49 PM. Reason: wrong code

      Comment

      • Sudaraka
        New Member
        • Jan 2011
        • 55

        #4
        Ok, so what you should do is for urls that goes to product[n].html pages capture the page name and append that to the query string like below
        Code:
        RewriteRule ^(product[0-9]+).htm /product.htm?%{QUERY_STRING}:$1 [R=301,L]
        Please note that for this to work your new page must not match the pattern we check for old files (product[n]) otherwise it creates an infinite loop.

        Comment

        • Stuart Hunter
          New Member
          • Jan 2011
          • 13

          #5
          I see, that code is fantastic thank you and would be great for the original URLs (and something I will be sure to use in future) but unfortunately the product1.htm is an example, the actual URLs have no similarity so it's separate lines for each instance :(

          Within that string Sudaraka would you mind pointing out where I would add :product_name to the new redirect string? Before the $1?

          i.e.

          Code:
          RewriteRule ^product1.htm /product.htm?%{QUERY_STRING}:product_name$1 [R=301,L]
          I assume the rewrite will then automatically pick up the product.htm file within the directory saving a redirect?

          Sorry about this, I am dusting off my apache and javascript knowledge lately to ensure best practice.

          Comment

          • Sudaraka
            New Member
            • Jan 2011
            • 55

            #6
            First, Yes, you can before or even after the $1 as you like.
            Please note that $1 refers to the matching blocks from the regular expression match of the current url, and since you have no brackets in there (^product1.htm) $1 will be empty.

            Second, if you like to do this without the redirect you can simple get red of the R=301 key, and if there are other url rewrites you do after this you should as PT to pass through the rewrite result done in this section.

            Code:
            RewriteRule ^(product1).htm /product.htm?%{QUERY_STRING}:product_name:$1 [PT,L]
            # or without the reference to old url
            RewriteRule ^product1.htm /product.htm?%{QUERY_STRING}:product_name [PT,L]

            Comment

            • Stuart Hunter
              New Member
              • Jan 2011
              • 13

              #7
              Perfect, you have a been a fantastic help, thank you so much.

              The only addition was to add 'RewriteEngine on'...

              Code:
              RewriteEngine on
              RewriteRule ^product1.htm /product.htm?%{QUERY_STRING}:Product [R=301,L]
              ...then that all worked a treat :)

              Comment

              • Sudaraka
                New Member
                • Jan 2011
                • 55

                #8
                Nice to hear it worked out for you. Glad to help.

                Comment

                Working...