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
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
Share on other sites

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."

Link to comment
Share on other sites

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.

 

Link to comment
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
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
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.