Eejit Posted July 26, 2009 Share Posted July 26, 2009 Hi, I'm new to mod_rewrite and the code and after many hours, seek help with a problem. I'm trying to compile code to insert into my .htaccess file that will redirect dynamic addresses. www.example.com/sub/page.php?ID=F123&range=001 needs to be redirected to: www.example.com/sub/page.php?ID=F123&range=kp1 I am having problems ensuring that the rewrite maintains the same ID in the query string whilst changing the range. So far I have: RewriteEngine On RewriteCond %{QUERY_STRING} ID=(.*)&range=001 RewriteRule ^sub/page.php http://www.example.com/sub/page.php?ID=%1&range=kp1 [R=301,L] As this doesn't work, any suggestions will be greatly received. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/167491-dynamic-redirect/ Share on other sites More sharing options...
lifetalk Posted July 26, 2009 Share Posted July 26, 2009 If I understand you correct, what you want to do, is keep the ID same.. but change the range from 001 to kp1 for every request, right? This code might work (not tested) RewriteEngine On RewriteRule ^/sub/page.php?ID=(.*)&range=001$ /sub/page.php?ID=$1&range=kp1 [R=301,L] I'm not sure if the '?' and '&' need escaping. Not tried it yet. Quote Link to comment https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-883330 Share on other sites More sharing options...
Eejit Posted July 26, 2009 Author Share Posted July 26, 2009 Thanks lifetalk. That's exactly what I'm trying to achieve. Unfortunately, your suggestion didn't work. I'm not sure I understand your closing comment, "I'm not sure if the '?' and '&' need escaping." Quote Link to comment https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-883346 Share on other sites More sharing options...
lifetalk Posted July 26, 2009 Share Posted July 26, 2009 Varied the above htaccess (apparently needs escaping) Try this: RewriteEngine On RewriteRule ^/sub/page.php\?ID=(.*)\&range=001$ /sub/page.php\?ID=$1\&range=kp1 [R=301,L] Quote Link to comment https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-883349 Share on other sites More sharing options...
Eejit Posted July 26, 2009 Author Share Posted July 26, 2009 Thanks, but that also failed. Have also tried: RewriteEngine On RewriteCond %{QUERY_STRING} ID=(.*)\&range=001 RewriteRule ^genealogy/getperson.php http://www.example.com/sub/page.php\?ID=$1\&range=kp1 [R=301,L] but this has failed too. Don't know why. Quote Link to comment https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-883373 Share on other sites More sharing options...
lifetalk Posted July 26, 2009 Share Posted July 26, 2009 I am apparently not able to think straight here. I know the solution to the above issue is really simple and straightforward, but my head is just not clear at this point. Try this code: RewriteEngine On Options +FollowSymlinks RewriteBase / RewriteCond %{QUERY_STRING} ^ID=(.*)&range=001$ RewriteRule ^/sub/page\.php$ /sub/page.php?ID=%1&range=kp1 [R=301,L] Quote Link to comment https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-883403 Share on other sites More sharing options...
Eejit Posted July 26, 2009 Author Share Posted July 26, 2009 Thanks for your help. Wasted too much time on this one and now experiencing the same thinking problems. Perhaps there's another way - another day! Quote Link to comment https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-883426 Share on other sites More sharing options...
lifetalk Posted July 26, 2009 Share Posted July 26, 2009 Tried the last code I pointed out? Quote Link to comment https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-883460 Share on other sites More sharing options...
Eejit Posted July 27, 2009 Author Share Posted July 27, 2009 The code didn't work. Through a process of trial and error, this code: RewriteEngine On Options +FollowSymlinks RewriteCond %{QUERY_STRING} ^ID=(.*)$ RewriteRule ^page\.php$ http://www.example.com/sub/page.php\?ID=%1&range=kp1 [R=301,L] Redirects www.example.com/page.php?ID=123 to www.example.com/sub/page.php?ID=123&range=kp1 This is the closest I have come to getting it to work. Now working on alterations to the last line to include the "/sub" in the URL of page to be directed. These are failing: RewriteEngine On Options +FollowSymlinks RewriteCond %{QUERY_STRING} ^ID=(.*)$ RewriteRule ^/sub/page\.php$ http://www.example.com/sub/page.php\?ID=%1&range=kp1 [R=301,L] This returns a 404 Error RewriteEngine On Options +FollowSymlinks RewriteCond %{QUERY_STRING} ^ID=(.*)$ RewriteRule ^sub/page\.php$ http://www.example.com/sub/page.php\?ID=%1&range=kp1 [R=301,L] With this, the server enters a redirection loop. Quote Link to comment https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-883777 Share on other sites More sharing options...
lifetalk Posted July 30, 2009 Share Posted July 30, 2009 I would probably be able to do it better if I actually had live examples. What PHP script are you using, if a publicly available one, let me know and I'll install it on my localhost and test it out. That way, with something 'live' for me I'd be able to test it 10x better. Quote Link to comment https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-886852 Share on other sites More sharing options...
Eejit Posted July 30, 2009 Author Share Posted July 30, 2009 The script isn't public and I have decided to give up on this for the moment. In the meantime I have developed a work around. Thanks for your time and interest with this, it has been useful. Quote Link to comment https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-887029 Share on other sites More sharing options...
lifetalk Posted July 31, 2009 Share Posted July 31, 2009 Ah well, I see. In either case, I'll do a quick (blank) script with such URL's and run a few tests on my localhost in a day or two. I know I'll find out a solution (it's simple) and I'll let you know once I do. Cheers, Hamza Quote Link to comment https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-887560 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.