-1

I have the directory abc and inside it I have two files. One is login.php and another one is index.php. I have path localhost/abc redirect to index.php but I want to redirect to login.php.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
john marker
  • 41
  • 1
  • 8

3 Answers3

2

By default a page will always open index.php file in a directory. This is the default behaviour. You can change it by changing the server settings but that depends on which server you are using eg. Apache IIS etc.

Assuming Apache you're using... you need to change the following line in httpd.config file in apache folder

<IfModule dir_module>
    DirectoryIndex index.php index.php3 index.html index.htm
</IfModule>

This line will look for index.php then if not found it will search for the successive files listed after it. You can change the order of files as per your needs.

Since you have tagged your question javascript you can redirect to the desired page without changing the server settings using the below code

<script type="text/javascript" language="javascript">
    window.location = 'login.php';
</script>

But i think you want to do something like if the user is not logged in then redirect to the login page that u can do as under..

Assuming you are using sessions and PHP again

//write this in index.php
if(isset($_SESSION["isLoggedin"]) && $_SESSION["isLoggedin"]==true){ //if user has logged in and session variable has been set.
    header("Location:login.php"); //this will redirect the page to login.php
}

Hope this helps...

LoneWOLFs
  • 2,306
  • 3
  • 20
  • 38
0

when visit localhost/abc it redirect to index.php

//index.php
//javascript method
if( !checkLogin() ){
    window.location.href = 'login.php'
}
//php way
if( !checkLogin() ){
    header('location: login.php');
}

checkLogin return false when you have not login.

Rex Huang
  • 85
  • 4
0

Why redirect with javascript if you can use PHP?

  • If javascript is disabled then the redirect won't work!
  • You have to encode the url in some cases
  • HTTP is far better suited to the job than JavaScript is
  • Search engines follow them
  • You can state if it is permanent or not

Redirect 302 with PHP

<? header('Location: /login.php'); ?>

The header function sends raw header data to the client web browser and in this case sends a 302 status code which instructs the browser to redirect to the given location. But it won't be as good SEO wise.

Community
  • 1
  • 1
Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85