Welcome Guest, Not a member yet? Register   Sign In
Everyone's favorite subject...URL rewriting [Solved]
#21

[eluser]kettch[/eluser]
[quote author="TheFuzzy0ne" date="1240535800"]Judging by what's in the user guide, it looks like the function needs to be within a class.[/quote]

According to the user guide:
Quote:The name of the class you wish to invoke. If you prefer to use a procedural function instead of a class, leave this item blank.

which is why I left the class parameter blank and just had the function. Just for kicks I enclosed the function in a class and added it to the hook definition and still no success.
#22

[eluser]TheFuzzy0ne[/eluser]
Whoops. Sorry, I missed that. I just had a quick look at the examples without reading the text.

I'm wondering when CodeIgniter destroys the $_GET array. If it's being destroyed before your script is executed, that would explain the problem. Maybe you need to work with $_SERVER['REQUEST_URI']?
#23

[eluser]kettch[/eluser]
I tried using $_SERVER['REQUEST_URI'] as well as $_SERVER['QUERY_STRING'] to see if they would work better than the $_GET array and had the same result...no dice.

Since the hook is being defined as pre_system then CI isn't even loaded at that point, so the $_GET array shouldn't be destroyed yet...unless I'm completely misunderstanding the process, of course.
#24

[eluser]Daniel Moore[/eluser]
On my original suggestion, I had to flush my browser's cache for some odd reason before it would begin to work. At first it would give a CI error that I had disallowed characters in the URI, which was ludicrous as it never should have made it to CI. After flushing the cache and reloading the page, it began to work as expected.

Give it a try and see if that helps. There is no reason it shouldn't work, because if you include those statements immediately after the <?php in index.php, then CodeIgniter should never get the chance to destroy the $_GET variable, as it never should reach that far.
#25

[eluser]kettch[/eluser]
Just for curiosity's sake I threw an echo just before the header($redirect); and $redirect is getting set to
Code:
Location: http://localhost/chlv2/page/id/home
which is the correct URL and if you copy that URL into the browser it loads correctly...I don't get why the header redirect is not actually redirecting.
#26

[eluser]Daniel Moore[/eluser]
Ok, 3 things we need to do here.

Number one, we need to add an "exit;" to prevent any code below the redirect from being executed. It is important to note that headers are actually sent when the first byte is output to the browser. If you are replacing headers in your scripts, this means that the placement of echo/print statements and output buffers may actually impact which headers are sent. In the case of redirects, if you forget to terminate your script after sending the header, adding a buffer or sending a character may change which page your users are sent to.

Number 2, we need to add the trailing slash.

The 3rd thing I neglected to point out is that some browsers will not redirect unless the Status is sent first. (Chrome, for example.) I have added this, and you can set the redirect status to suit your needs.

"Location:" must be capitalized, which you already have, as some browsers will not redirect if it is not.

Try this:
Code:
<?php
if (isset($_GET['view']))
{
  $redirect = 'Location: http://example.com/page/id/'.$_GET['view'].'/';
  header("Status: 301");
  header($redirect);
  exit;
}
and change example.com to your site. And clear the browser cache when you test it, just in case.
#27

[eluser]kettch[/eluser]
Excellent Daniel, those changes make it work perfectly. I actually narrowed it down to just needing to add the "exit;" at the end in order to make it work, but I'm still going to keep the rest to make sure, as you mentioned, that the redirect occurs properly in all browsers.

Thanks much! Problem solved.
#28

[eluser]xwero[/eluser]
[quote author="kettch" date="1240532816"]Even adding xwero's solution as a pre_system hook doesn't redirect. Augh![/quote]
I just tested it with the code you showed and it worked for me.
#29

[eluser]Daniel Moore[/eluser]
Yes, the new code should work as a pre-system hook as well.

Congratulations. I hope several people will be able to use it in one way or another.
#30

[eluser]kettch[/eluser]
Yeah I have the new code in a presystem hook and it works great. Thanks guys! I hope someone else can use this trick, too!




Theme © iAndrew 2016 - Forum software by © MyBB