473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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?
Jan 11 '11 #1

✓ answered by Sudaraka

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.

Expand|Select|Wrap|Line Numbers
  1. RewriteRule ^(product1).htm /product.htm?%{QUERY_STRING}:product_name:$1 [PT,L]
  2. # or without the reference to old url
  3. RewriteRule ^product1.htm /product.htm?%{QUERY_STRING}:product_name [PT,L]

7 5256
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:
Expand|Select|Wrap|Line Numbers
  1. RewriteRule (old_page.html) new_page.php?redirected_from=$1 [QSA]
Jan 11 '11 #2
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?01234:user_name_one"
"http://www.test.com/product2.htm?43210:user_name_two"

new urls=
"http://www.test.com/product1.htm?01234:user_name_one:product_name"
"http://www.test.com/product1.htm?01234:user_name_two:product_name"
Jan 11 '11 #3
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
Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 11 '11 #4
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.

Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 11 '11 #5
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.

Expand|Select|Wrap|Line Numbers
  1. RewriteRule ^(product1).htm /product.htm?%{QUERY_STRING}:product_name:$1 [PT,L]
  2. # or without the reference to old url
  3. RewriteRule ^product1.htm /product.htm?%{QUERY_STRING}:product_name [PT,L]
Jan 11 '11 #6
Perfect, you have a been a fantastic help, thank you so much.

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

Expand|Select|Wrap|Line Numbers
  1. RewriteEngine on
  2. RewriteRule ^product1.htm /product.htm?%{QUERY_STRING}:Product [R=301,L]
...then that all worked a treat :)
Jan 11 '11 #7
Nice to hear it worked out for you. Glad to help.
Jan 11 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dmitry Karneyev | last post by:
Hi All! I've got an ado.net app with several win forms. In the first form (form1) I connect to DB and in other forms I use connection string generated by the form1 and stored in sqlconnection...
1
by: VB Programmer | last post by:
I have a default.aspx page that simply does this (in the "Try" block): Response.Redirect("MyStartPage.aspx") I keep getting this exception on this line: "System.Threading.ThreadAbortException:...
8
by: msnews.microsoft.com | last post by:
I want to redirect the user to a url outside of our website but I want it to preserve our application's window by opening a new window. We have a datagrid that has five hyperlink columns containing...
3
by: Peter Row | last post by:
Hi, I better get the background stuff out the way first, so here goes: - Porting a VB6 webclass app to VB.NET using HttpHandlers and FormsAuthentication - When someone visits my site...
1
by: EagleRed | last post by:
I am writing an ASP.NET application in which I am using the Response.Redirect() call. I noticed that I catch a ThreadAbort Exception after making the call. Indeed the documentation states that this...
3
by: neil_pat | last post by:
I have a stored procedure which returns a value of between 0 and 4. I want the user to press a button to receive feed back on their last input. The save button takes the input and saves it to...
3
by: Peter Kirk | last post by:
Hi I need to write a method which accepts a url (string) and redirects to it. In addition to this I need to either (a) add a parameter to the url; or (b) add the parameter to the header of the...
2
by: Jason Huang | last post by:
Hi, In my .Net 2.0 C# web form Form1, say I have a button btnToForm2. Is the Request.Redirect("Form2") the right way to go to the Form2 in the btnToForm2_Click event? Actually, I also want to...
56
by: UKuser | last post by:
Hi, I'm not sure if this can be done as I've searched the web and this forum. I am using an online merchant provider and I must post certain variables to their webforms through a form on my...
3
by: Julien | last post by:
Hi, I can't seem to find the right regular expression to achieve what I want. I'd like to remove all characters from a string that are not numbers, letters or underscores. For example: ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.