Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite query

Help with getting a small mod rewrite script to work

         

nathanpitman

8:11 pm on Sep 28, 2005 (gmt 0)



Hi all, I've been having a tinker with mod rewrite, amazingly powerful stuff. Some of my tests seem to work but this single one will not. Any help most appreciated.

I have a page on my domain 'news.php' which dynamically serves news, or it did, then I moved it to '/NewsAndFeatures/index.php'.

When the page is called it's passed a querystring in the format:

index.php?news_ID=43

What I need to do is route all incoming requests for news items at the old address...

news.php?doc_ID=43

...to the new address...

/NewsAndFeatures/index.pgp?news_ID=43

I 'thought' that this would do the trick, but something in the originating URL is causing it to break.

RewriteRule ^news\.php?doc_ID=([0-9][0-9])$ NewsAndFeatures/index.php?news_ID=$1 [R]

Any ideas?

jdMorgan

9:40 pm on Sep 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



nathanpitman,

Welcome to WebmasterWorld!

You'll need to use RewriteCond to handle the query string. Query strings are not considered part of the URL, but rather, data to be passed to the resource *at* the given URL. Therefore, query strings are not 'visible' to RewriteRule for pattern-matching.

For use in .htaccess:


RewriteCond %{QUERY_STRING} ^doc_ID=([0-9]{2})$
RewriteRule ^news\.php$ http://www.example.com/NewsAndFeatures/index.php?news_ID=%1 [R=301,L]

Here, the 2-digit doc_ID number stored by RewriteCond is back-referenced using %1 in RewriteRule.

Jim

nathanpitman

12:08 pm on Sep 29, 2005 (gmt 0)



Worked a treat! Thanks Jim, you are a star!