Forum Moderators: phranque

Message Too Old, No Replies

Redirect 1 dynamic page to another

Using httpd.conf on a virtual host

         

burcot

1:41 am on Feb 22, 2006 (gmt 0)

10+ Year Member



Hi

I have tried numerous rewrite variations in an attempt to correctly redirect a particular set of dynamic pages to another set. This is necessary due to a site update.

The background is that I am using the httpd.conf file to control mod_rewrite on a virtual host.

I am looking to redirect specific individual dynamic urls in this format...

/proddetail.php?prod=ABC003&cat=106

to specific individual dynamic urls in this format...

/proddetail.php?prod=ABC003&cat=12&nav=11

Each existing dynamic page has a corresponding dynamic page in the new site structure. While the 'prod=' value will match in all cases, the 'cat=' value is almost certain to change and the 'nav=' value is an entirely new addition.

On this basis, does anybody have any suggestions on how to successfully achieve these individual redirects?

Many Thanks

jdMorgan

2:10 am on Feb 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use mod_rewrite to look up the new URLs in your products database, using RewriteMap. Note that the map itself must be defined in the Server or VirtualHost configuration file. This may not be an option for you, so check with your hosting provider; Most hosting companies won't offer this level of customization unless you are on a virtual private server or a dedicated server.

If you cannot use RewriteMap, then you could rewrite all incoming requests that match the old URL/query string layout to a script that looks up the new URL/query using the old URL/query as the key value, and then generate the redirect inside the script itself. PHP or PERL would be well-suited to this task.

Generally, mod_rewrite alone without the RewriteMap function or an additional scripted extension is not very useful if the new URL/query cannot be directly-derived from the old URL/query using a simple fixed-formula text substitution or rearrangement.

Jim

burcot

2:16 am on Feb 22, 2006 (gmt 0)

10+ Year Member



Hi Jim, thanks for your response.

I am on a private server but unfortunately have no experience of RewriteMap.

Is there no solution whereby I can reference specific dynamically generated values, along the lines of id=([115]) for instance?

Many Thanks

burcot

4:28 am on Feb 22, 2006 (gmt 0)

10+ Year Member



Ok

I think I can work around having to create all the new category references etc so would like to simplify my initial question.

Can anybody please advise me how I can use the rewrite to drop the final '&nav=' part of the url from addresses such as...

/proddetail.php?prod=ABC003&cat=12&nav=11
/proddetail.php?prod=DEF005&cat=09&nav=13
/proddetail.php?prod=GHI007&cat=10&nav=6

...thus resulting in URLs like...

/proddetail.php?prod=ABC003&cat=12
/proddetail.php?prod=DEF005&cat=09
/proddetail.php?prod=GHI007&cat=10

Any input would be greatly appreciated!

jdMorgan

4:38 am on Feb 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are those name/value pairs always in the same order?

Jim

burcot

5:00 am on Feb 22, 2006 (gmt 0)

10+ Year Member



Hi Jim, thanks for replying again

These are example values, but the queries always appear in the same sequence i.e prod= cat= nav=

'prod' relates to products, 'cat' to categories and 'nav' to navigation.

The prod value will always change as they are unique to each individual product.

However, a range of 5 or more cat values can share the same nav value e.g

cat=19&nav=10
cat=20&nav=10
cat=21&nav=10

I hope that is what you were trying to determine?

jdMorgan

5:22 am on Feb 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good, that makes it much simpler.

RewriteCond %{QUERY_STRING} ^(prod=[^&]+&cat=[^&]+)&nav=
RewriteRule /proddetail\.php$ http://www.example.com/proddetail.php?%1 [R=301,L]

Note that "%1" back-references the value matching the parenthesized sub-expression in the RewriteCond pattern.

The "[^&]+&" pattern means, "Match one or more characters not equal to an ampersand, followed by an ampersand."

This will work as long as the 3 name/value pairs are always present in the original request, and always in the same order.

Note that the only thing prevent an 'infinite' rewrite loop is the presence of the "&nav=<anything>" name/value in the original request's query string, and its absense in the redirected URL. If that ever changes, you might want to rename your script to proddetail2.php or something different to prevent a loop.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

burcot

11:53 am on Feb 22, 2006 (gmt 0)

10+ Year Member



Many thanks Jim

This works fine and has given me a better understanding of the rewrite command.