Jump to content

Dynamic redirect


Eejit

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/167491-dynamic-redirect/
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-883330
Share on other sites

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]

Link to comment
https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-883403
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/167491-dynamic-redirect/#findComment-883777
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.