4

I have this htaccess filecontent:

Options +FollowSymLinks 
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^admin/([a-zA-Z]+)/?([a-zA-Z0-9/]*)$ public/admin/index.php?page=$1&query=$2 [L]
RewriteRule ^([a-zA-Z]+)/?([a-zA-Z0-9/]*)$ public/index.php?page=$1&query=$2 [L]

And now im trying to make a redirection depending on a session, very simple stuff. I have a Controller with this function:

protected function redirect($url) {
    header("Location: http://localhost:8888/myproject/" . $url, true);
    exit(); 
}

The ClientController that extends Controller has the page function

private function user_page() {
   $this->redirect("homepage");  //Not working...
}

I have tried different ways of writing the location path like

Location: localhost:8888/myproject/" . $url,
Location: /myproject/" . $url,

Now after reading all posts on stackoverflow, I see none of these solutions have worked for me..


Edit: the solution to this can be seen on header redirect to Location does not work

Community
  • 1
  • 1
patriques
  • 5,057
  • 8
  • 38
  • 48
  • Is error reporting enabled ? Are you getting errors ? Are you sending html/data before `header()` ? – HamZa Jul 07 '13 at 08:17
  • do you have an error message ? – Miguelo Jul 07 '13 at 08:20
  • I have set ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); inside of the index.php for reporting, and no Im not sending data before or after, since I want to see it working before I continue. TDD man! – patriques Jul 07 '13 at 08:22
  • And im not getting any error report messages... – patriques Jul 07 '13 at 08:23
  • ok what happens when the function is called – Miguelo Jul 07 '13 at 08:23
  • @patriques Comment out this line `$this->redirect("homepage"); //Not working...` and try `echo "redirect";`, what do you get ? – HamZa Jul 07 '13 at 08:24
  • @HamZa Good tip, I have actually tried and seen that it enters there, but I did it again anyway and as expected I got "redirect" displayed on my browser, and thats all I saw as Im actually not outputting anything else but trying the redirectfuntion to work. – patriques Jul 07 '13 at 08:27
  • @Miguelo nothing happens it stays on the user_page that is blank currently. – patriques Jul 07 '13 at 08:28
  • @patriques ok, now try to comment out `header("Location: http://localhost:8888/myproject/" . $url, true);` and put `echo "redirect";` – HamZa Jul 07 '13 at 08:29
  • @HamZa That I had not tried but now I see that the function call actually works, so it wasn't that either. – patriques Jul 07 '13 at 08:32
  • @patriques what does `$url` contain `var_dump($url);` ? Also are you running your server on port 8888 ? – HamZa Jul 07 '13 at 08:36
  • @HamZa Im running var_dump and get string(13) "homepage". Im running on port 8888, is that a problem with redirection? – patriques Jul 07 '13 at 08:40
  • @patriques nope just to confirm that `$url` is valid. I can only think that your .htaccess is doing something wrong. That last line, it will match your "redirected" page. Are you sure that the folder `public` exists with `index.php` in it ? – HamZa Jul 07 '13 at 08:44
  • @HamZa I think, it's not .htaccess. It doesn't refresh the page at all. So, I think, it's php redirect, that doesn't work. – user4035 Jul 07 '13 at 08:48
  • 1
    @user4035 `header() doesn't work` is just impossible unless you've compiled php without it lol – HamZa Jul 07 '13 at 08:51
  • @patriques I tried to imitate your error, creating a file with 2 functions - and it worked. Something is happening before these functions are called. Maybe, some other headers are sent, not letting this code to work correctly. The cause of the error is not in the functions, you posted. – user4035 Jul 07 '13 at 08:52
  • @HamZa header works of course, something else is blocking it. Try to create a simple file with 2 functions and call user_page(); - it will work. – user4035 Jul 07 '13 at 08:54
  • @user4035 After the debugging we have tried, the function does work, header does work. Now the redirected url is matched with that last line in .htaccess. The "redirected" page isn't `http://localhost:8888/myproject/homepage`, but it becomes `http://localhost:8888/myproject/public/index.php?page=homepage&query=` – HamZa Jul 07 '13 at 08:56
  • @user4035 Im looking into why it isn't running, its pretty weird. Im running on MAMP and well I will try settning up a dummy project to see if it is somthing else. – patriques Jul 07 '13 at 09:02
  • @HamZa Yes it is supposed to do this since its specified in the .htaccess file. So when I write in the header the index.php is supposed to interpret the query and what query im doing. – patriques Jul 07 '13 at 09:04
  • @patriques Please, rename .htaccess and see, whether it will redirect without it. Will it give you 404 or not. Then we can eliminate .htaccess question. – user4035 Jul 07 '13 at 09:05
  • @user4035 Now I tested the functionality without the htaccess. So I was writing the calls for pages as HamZa was describing before: `http://localhost:8888/myproject/public/index.php?page=homepage&query=` and that did not make the redirections work either so it seems to be some other error. Maybe in my other functions? – patriques Jul 07 '13 at 09:24
  • @patriques Yes, it is in some other function. – user4035 Jul 07 '13 at 09:34
  • @user4035 Now I made a simple `` in another folder on my MAMP server and still I cant make a redirect? Now I see it is the header function that does not work.. I think I will make a separate question.. – patriques Jul 07 '13 at 09:53
  • possible duplicate of [header redirect to Location does not work](http://stackoverflow.com/questions/17511072/header-redirect-to-location-does-not-work) – mario Jul 07 '13 at 10:18
  • open in a hex editor and check for a unicode byte order mark (U+FEFF) at the start, this can cause problems in some older phps – user1937198 Jul 07 '13 at 10:44
  • @patriques So how are things going ? – HamZa Jul 07 '13 at 15:31
  • @HamZa The problem was some row of spaces that had been inputted after ´?>´ in the Controller, so it took a while to notice it. But that was it! I saw it first when looking for spaces in the files and saw that there where linenumbers after the `?>` although no code was written. Thanks for asking, and thanks for your collaboration :) – patriques Jul 07 '13 at 18:59
  • @patriques hehe which confirms my first comment `Are you sending html/data before header() ?`. It's a common error. Post the solution as an answer and accept it. That way, we don't have an unanswered question. Take care. – HamZa Jul 07 '13 at 20:37

1 Answers1

1

After getting an answer from header redirect to Location does not work I found that the problem was some row of spaces that had been inputted after ?> in the Controller, so it took a while to notice it.


Clarification: If you have the same problem make sure you have line numbers set and see if you can see any line numbers after the file content. If there is you may have blank lines after aswell. Just delete these. Needless to say, the same goes for linenumbers before filecontent

Community
  • 1
  • 1
patriques
  • 5,057
  • 8
  • 38
  • 48