0

I want to redirect the user when he visits the homepage.

So if he goes to example.com, Of course if there is http,https,www or visiting from google, He should be redirected to example.com/#element.

I can't use:

header()

Because the website is built with CodeIgniter and I can't get the right php file.

Either:

window.location

As I don't want to rely on the client side.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • 2
    Do it with JavaScript (location.hash is the property you want to use), everything else is going to lead into trouble in this specific constellation. While server-side redirecting to a URL with a hash part is possible, here you would be redirecting from `/` to `/#element` - but if that makes the client make an actual new request to the server, you will be caught in an endless loop, because the hash part of the URL is not send to the server by most clients, so you would have no way to distinguish this request from the previous one. – CBroe Jul 12 '18 at 18:09
  • What about .htaccess? Would it lead ta a problem? –  Jul 12 '18 at 18:19
  • That would also be server-side redirecting, so - same problem. – CBroe Jul 12 '18 at 18:23
  • What is the problem? Can't it for example check if that exists? or just redirect to that URL –  Jul 12 '18 at 18:24
  • I mean if it's redirecting from the homepage, If that part exists, It will redirect to the same page without adding that part again –  Jul 12 '18 at 18:25
  • _"Can't it for example check if that exists?"_ - check if _what_ exists? The problem is, as I already tried to explain, that from the server's perspective the two requests that reach it will both look the same: `/` - now you tell me how you would want to decide that you would want to redirect the "first", but not the "second" one ...? (In quotes, because there is no first and second either - it's just two requests that have nothing to do with each other. That's how the _stateless_ protocol HTTP works. You'd have to introduce state, f.e. via cookies - but that would be unreliable as well.) – CBroe Jul 12 '18 at 18:32
  • I mean if that part `#element` exists, I saw .htaccess code that contains /page.html? at the beginning, Doesn't that ? at the end represents any thing after page.html? –  Jul 12 '18 at 18:40
  • No, it doesn't. The query string ends where the fragment (a.k.a. hash, the `#foo` part) starts. Read up on some basics maybe? https://en.wikipedia.org/wiki/URL#Syntax – CBroe Jul 12 '18 at 18:44
  • 1
    Few days old, but what I think you're looking for is [this solution](https://stackoverflow.com/questions/723883/redirect-with-codeigniter). – MinistryOfChaps Jul 16 '18 at 09:37

1 Answers1

0
$this->load->helper('url');   
redirect('/controller/function/', 'refresh');

use this code in index function of your home controller

Shafeer khan
  • 464
  • 5
  • 8