htaccess does not load CSS with second parameter

Asked

Viewed 808 times

0

Hello. I have been breaking my head too much to try to solve a problem that should be very simple but is not working.

My website is browsed as follows on htaccess localhost/ProjetoBusca/index.php?pg=anuncio being replaced by localhost/ProjetoBusca/anuncio. So far it works perfectly. The problem is that I need a secondary GET, where the ideal would be to transform localhost/ProjetoBusca/index.php?pg=anuncio&n=354 being replaced by localhost/ProjetoBusca/anuncio/354

For this, in my htaccess I developed:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(.*)$ index.php?pg=$1&n=$2 [NC]
RewriteRule ^(.*)\/(.*)/$ index.php?pg=$1&n=$2 [NC]

When I work only with /announcement on the site (as mentioned above) everything works normal, the problem is that when I call /advertisement/354 I can get the n variable get, but the full page CSS does not load. What might be going on?

Follow the code of index.php:

<html>
    <head>
        <?php   include("view/header.php"); ?>
    </head>
    <body>      

        <?php      

            include("view/topbar.php");
            include("view/navbar.php");

            if(isset($_GET['pg'])){
                $url = $_GET['pg'];                                     

                if(!file_exists("$url.php")){
                    include("home.php");
                }else{
                    include("$url.php");
                }
            }else{
                include("home.php");
            }

            if(isset($_GET["n"])){
                echo "foi instanciado n: ".$_GET["n"]; //para fins de teste
            }

            include("view/rodape.php");
            include("view/copyright.php");
            include("view/scripts.php");?>
    </body>
</html>

Could someone help me?

NOTE: the include with view/... is because they are in another folder.

  • It happens because the link /anuncio/354 is interpreted as directories, and then the css is searched within them(/anuncio/354/css/style.css)

1 answer

2


Well, this is a fairly common problem when using urls amigáveis, there are several ways to solve it, the most basic would be to request your css with an absolute link:

<link href="http://www.meusite.com/css/style.css" rel="stylesheet">

There are more dynamic ways to do this, to get the site url:

<?php define('SITE_URL', ' http://localhost/ProjetoBusca'); ?>

Then to get this url you would use:

<link href="<?php echo SITE_URL; ?>/css/style.css" rel="stylesheet">

You can also use $_SERVER to fetch this url:

$baseUrl = dirname($_SERVER['PHP_SELF']).'/';

Source: stackoverflow

You can also use a base tag:

<base href="http://www.meusite.com/" />
<link rel="stylesheet" href="style.css" type="text/css" />

Either way you will have to have an absolute path to order.

Source: http://pumka.net/

  • It worked, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.