Home > database >  Serving index.php and redirecting it to static pages
Serving index.php and redirecting it to static pages

Time:04-14

I am trying to serve index.php and redirect it to static pages:

index.php

<?php include_once("my_folder/index.html"); ?>

But it shows the html page with missing images, broken links, and no embedded styles. Then I realized that it is because I need to include the other files in the directory—specifically html and css files and a folder containing the images. So i changed the content into:

index.php

<?php
foreach (glob("my_folder/*") as $filename)
{ 
    include $filename;
}
?>

But then it shows all html files in one page, still with broken links and without the images and embedded styles.

What should my index.php contain for it to work properly?

CodePudding user response:

The include and require functions also their _once versions simply take content of the file and place it where they are called, neither of them act like redirect. From browser view the links in your html file are relative to the folder in which they are placed but with include you are still in root folder, that is why the css and images aren't loaded, you would have to make links in html and css absolute (or relative to root), then it would work as you expect.

If you really wanna use redirect then you can do it this way:

header('Location: /my_folder/index.html');
exit;

which produce 302 (temporary) redirect.

Keep in mind that since you are sending header it will only work when you do not sent any output before header call, because at that moment all headers are already sent and you cannot add more.

  • Related