0

I want to redirect all traffic to one page where I decide what to output based on the URL the user entered. So far it works fine, the only thing that does not work is loading the stylesheet. Currently I tried all of it locally with XAMPP.

I reduced the code to a minimum to point out the problem.

.htaccess:

RewriteEngine on

RewriteRule ^((?!test\.php).+)$ /test.php [L]

Everything except test.php is rewritten to test.php

test.php:

<?php

$path = $_SERVER['REQUEST_URI'];

// remove any slashes in front of the path
$path = ltrim($path, '\\');
$path = ltrim($path, './');

if (pathinfo($path, PATHINFO_EXTENSION) == "css") {
    readfile($path);
}
else {
    echo("<!DOCTYPE html>\n");
    echo("<html>\n");
    echo("<head>\n");
    echo("<link rel='stylesheet' href='/stylesheets/test-style.css' />\n");
    echo("</head>\n");

    echo("<body>\n");
    echo("<h2>Test Heading</h2>\n");
    echo("</body>\n");

    echo("</html>\n");
}

?>

I check the file extension, if it is CSS, I output the file, if not I write some HTML code line by line.

test-style.css:

h2 {
    color: yellow;
}

If I open any address in the browser, the HTML is shown but the stylesheet is not applied. Everything else works (I can load a favicon, javascript works), but not the stylesheet. I could directly output the code inside '' tags but of course, that is not a good solution. If I use the inspector of firefox I see that the stylesheet is there and if I change a line it suddenly applies.

Any ideas why this is not working or maybe a suggestion on how to achieve the desired results in a different way? Cleaning the browser cache or changing browser has no effect.

9
  • Wouldn't the style sheet be before any other HTML. Commented Oct 8, 2019 at 15:17
  • You are reading the CSS file and outputting it to the browser BEFORE you output the DOCTYPE etc. Commented Oct 8, 2019 at 15:19
  • Look at where you output it in the ELSE ???? Commented Oct 8, 2019 at 15:19
  • Also if you get a css extension, you dont output any HTML so there is nothing to see on the page Commented Oct 8, 2019 at 15:23
  • 1
    you probably need to send the correct headers like e.g. content-type Commented Oct 8, 2019 at 15:27

1 Answer 1

1

@apokryfos found the solution: I had to send the content-type before outputting the CSS.

header('Content-type: text/css');

Thank you all for your help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.