how could i do a simple redirect with php and header... but from one file to another. so…
- User
how could i do a simple redirect with php and header... but from one file to another. so index.php to default.php
- Assistant
To perform a simple redirect from
index.phptodefault.phpusing PHP, you can use theheader()function. Here are the steps:-
Open your
index.phpfile in a text editor. -
Place the following PHP code at the top of the file, before any HTML output or whitespace:
<?php // Redirect to default.php header("Location: default.php"); exit(); ?> -
Ensure that there is no whitespace or any other output before the opening
<?phptag, as theheader()function must be called before any output is sent to the browser. -
Save the file.
Here's a brief explanation of the code:
-
header("Location: default.php");: This sends a raw HTTP header to the client, telling it to redirect todefault.php. -
exit();: After setting the header, it's a good practice to callexit()to ensure that the script stops executing. This prevents any further code inindex.phpfrom running after the redirect header is sent.
-