I can not link to css with relative path

0

I'm using Valet and my link to the css file is not working. I have this in my file /exemplo/index.php

<link rel="stylesheet" type="text/css" href="css/style.css">

The file is in /exemplo/css/style.css . The browser attempts to load the file /css/style.css .

I just did another test here and noticed that when http://local.dev/example access does not work. But when http://local.dev/example/index.php access works.

Until that makes sense, but is there any way to make it work for both? Not even a function of php.

    
asked by anonymous 26.12.2016 / 23:44

1 answer

2

Relative path is a problem in the scenario described. The 2 mentioned URLs are in fact in different ways, this "relative" would be which?

http://local.dev/example
http://local.dev/example/index

In this case the relative root is less problematic (called absolute, but I believe it is not the best terminology because it does not include the domain).

An alternative, if you need to "relativize" yourself, is to force the script to always have a slash at the end (or never, adjusting it in the CSS path).

Something like this:

<?php
    $caminho = $_SERVER['PHP_SELF'];
    if( substr( $caminho, -1 ) !== '/' && substr( $caminho, -4 ) !== '.php' ) {
        header( 'Location: '.$caminho.'/' );
        die();
    }

If you need to use query string you can add in header also, concatenating after the slash with a ? .

Depending on the server and configuration, it may be the case to change PHP_SELF to PATH_INFO .

    
27.12.2016 / 00:20