Forum Moderators: phranque
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
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
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!
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?
RewriteCond %{QUERY_STRING} ^(prod=[^&]+&cat=[^&]+)&nav=
RewriteRule /proddetail\.php$ http://www.example.com/proddetail.php?%1 [R=301,L]
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