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

[eluser]xwero[/eluser]
CodeIgniter isn't loaded when the pre_system hook is triggered. The benefit of using a pre_system hook is that you can use the config_item function already for the redirection so your if can look like this
Code:
if (isset($_GET['view']))
{
  $redirect = 'Location: '.config_item('base_url').'page/id/'.$_GET['view'];
  header($redirect);
}
if you need this for multiple sites you can abstract it even more and add site specific params to the function call in the config/hooks.php file.

If you got a mess of urls then the nice function you wrote to make it easier to convert sites to CI can be too limiting and you have to do it in the index.php file.
#12

[eluser]Daniel Moore[/eluser]
That's true, but you're still loading more lines of code that way than you would if you were doing it my original way. Either way would work, but on a heavy traffic site, I would go with the least intensive way of doing it. It's rare for me to have to convert a site like this anyway, so I just chose not to go through the trouble of defining "base_url" twice because of a redirect.

If it's not a high traffic site, then it would indeed be best to make it a hook, especially if you foresee doing this for multiple sites. I generally only create new sites. The ones I converted were sites I had done in the past before I started using CI. For these reasons, I don't foresee me needing to do it for multiple sites in the future, but you never know.
#13

[eluser]xwero[/eluser]
True it loads more lines but if you want more performance you can make the function lower level by adding a base_url param and a config_item check and include the function file in the index.php file.

I can understand your situation but i think this is a situation that will happen again sooner or later and i don't want to code the same thing twice. If it's a single or a few checks hardcoding them is a valid option but if there are a lot of checks you are going to use some loop not to end up with all the variations in the urls as if(else)-s.

There is going to be a difference in performance but i'm not sure if it's significant enough to matter?
#14

[eluser]Daniel Moore[/eluser]
For 99.9% of sites, the performance issue won't matter. You are right about that. As I stated, if it's not a high traffic site, then it would indeed be best to do it your way on that one. For a site with millions of hits, you will need the most efficient code possible, because every line that has to be loaded by the server is another hit on that server's processor. Eventually it will catch up with you if the site is a high traffic site.

For lower traffic sites, it's all just a matter of personal taste, but would be best to put it in the hook as you suggest.

Even on a high traffic site, if you decide to do it in the index.php file, there should be some sort of documentation, perhaps in the hooks.php file, that would point you back to what you did should you need to revisit this in a couple of years, or should another programmer take over for you and need to figure it out.

Using a pre-hook would be the proper "CodeIgniter" way to do it for sure.
#15

[eluser]kettch[/eluser]
That's an interesting solution Daniel, I hadn't thought of that. I put it in and changed the url to mine but it do
#16

[eluser]kettch[/eluser]
That's an interesting solution Daniel, I hadn't thought of that. I put it in and changed the url to mine but it doesn't do the redirect.

I know this has got to be possible via the htaccess but this is driving me crazy. I wish the damned RewriteLog would work to try and track down the issue. I've never had such an issue with doing a simple redirect before. I'm even more confused as to adding Daniel's suggestion into the index.php file isn't working, either.
#17

[eluser]kettch[/eluser]
Even adding xwero's solution as a pre_system hook doesn't redirect. Augh!
#18

[eluser]TheFuzzy0ne[/eluser]
Have you enabled hooks in the config.php file?
#19

[eluser]kettch[/eluser]
[quote author="TheFuzzy0ne" date="1240532914"]Have you enabled hooks in the config.php file?[/quote]

Yup:

Code:
$config['enable_hooks'] = TRUE;

In the hooks.php I have:

Code:
$hook['pre_system'] = array(
    'class'    => '',
    'function' => 'URLRewrite',
    'filename' => 'URLRewrite.php',
    'filepath' => 'hooks'
);

And in the URLRewrite.php file I have:

Code:
<?php
function URLRewrite(){
    if (isset($_GET['view']))
    {
      $redirect = 'Location: '.config_item('base_url').'page/id/'.$_GET['view'];
      header($redirect);
    }
}
?>

This is my first time using hooks so maybe I'm doing something wrong?
#20

[eluser]TheFuzzy0ne[/eluser]
Judging by what's in the user guide, it looks like the function needs to be within a class.




Theme © iAndrew 2016 - Forum software by © MyBB