CodeIgniter Forums
How to remove GET parameters to avoid a 404? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to remove GET parameters to avoid a 404? (/showthread.php?tid=3037)



How to remove GET parameters to avoid a 404? - El Forum - 09-06-2007

[eluser]mipa[/eluser]
Hi,

We're migrating our old site to CodeIgniter and are using the Redirect directive in Apache to handle the redirects to our new pages. Our QA just discovered that the Redirect directive will add all the old links' GET parameters to the new CI urls and cause the new CI links to die a nasty 404 death.

So, the question is: what's the best way to chop the GET parameters before CI chokes? Should I use a hook? Are there any suggested or tested / proven solutions to this problem?

Any help is appreciated!


How to remove GET parameters to avoid a 404? - El Forum - 09-06-2007

[eluser]mipa[/eluser]
Ok, I think we have to handle this in the .htaccess file.

This is the key line:
Code:
RewriteRule ^(.*)$ index.php/$1 [L]

If I change it to this, it simply doesn't work and 404s before it even gets to CI.
Code:
RewriteRule ^(.*)\?(.*)$ index.php/$1 [L]

Also doesn't work:
Code:
RewriteRule ^(.*)(\?)(.*)$ index.php/$1 [L]

I'm trying to find the question mark and GET parameters and just not pass them to CI. Any one with an effective rule in this case?


How to remove GET parameters to avoid a 404? - El Forum - 09-06-2007

[eluser]mipa[/eluser]
Many more attempts to do this through .htaccess have been futile, so I'm going to look into the hooks method. Does anyone know how to access the URL in the pre_system stage?


How to remove GET parameters to avoid a 404? - El Forum - 09-06-2007

[eluser]Nillian[/eluser]
Hey there, I did something similar not too long ago, where I was reformatting the URL (with GET query) into CI-friendly URLs. If you use a pre-system hook, you won't have any CI stuff to work with, really. This comes before routing letalone extra libraries/helpers. So, I just used straight PHP to do it. Grab the $_GET variable and do what you want to it (in your case nothing) and then send a Header("Location: [your-page]").

However, I'm a noob so take this advice with a pinch of salt Smile

As for accessing the URL with PHP -> parse_url() is probably what you're after.


How to remove GET parameters to avoid a 404? - El Forum - 09-06-2007

[eluser]andreagam[/eluser]
Hi Mipa,

Try this simple trick: change this line in the config.php:
from
Code:
$config['uri_protocol']  = 'AUTO';

to
Code:
$config['uri_protocol']  = 'PATH_INFO';

This hint was from Joeles and helped me.


How to remove GET parameters to avoid a 404? - El Forum - 09-06-2007

[eluser]mipa[/eluser]
Thanks, mate! That was a big part of us finding a solution. In the end, we changed to PATH_INFO and then we also used a "pre_system" hook to analyze the REQUEST_URI for GET parameters and take the appropriate action.

Cheers.