Friendly URL changing file link

2

I have the following friendly url

RewriteRule ^noticias/([0-9]+)/?$ inicio.php?pg=noticias&id=$1

But when I access mysite.com/news/14

It changes the links of the files to

noticias/css/principal.css

where the original is

/css/principal.css

And when I access the link without the friendly example url

meusite.com.br/inicio.php?pg=noticias&id=$1

It works perfectly.

    
asked by anonymous 21.10.2015 / 05:46

2 answers

1

Your CSS should have a relative path and will be interpreted from the last page address bar

<link rel="stylesheet" href="css/principal.css"/>

This way depending on which page to open the address can be interpreted in different ways:

  • http://meusite.com.br/noticias/css/principal.css
  • http://meusite.com.br/css/principal.css

To work around the problem there are two solutions:

1- Put full address in CSS

 <link rel="stylesheet" href="http://meusite.com.br/css/principal.css"/>

2- Use the HTML element <base>

To set the base reference of all links (CSS, JS, links and images)

<base href="http://meusite.com.br/"/>

In this way, all relative paths will not depend on the page address.

    
21.10.2015 / 13:54
2

You are currently inserting your CSS file in a relative way, ie:

<link rel="stylesheet" href="/css/principal.css">

But in cases of friendly URL you should put CSS (and other assets like js and images) using an absolute path, which in this case would look like this:

<link rel="stylesheet" href="http://meusite.com.br/css/principal.css">

This is because the browser will understand the friendly URL as a directory, and will end up looking for the CSS inside that directory that does not exist ...

    
21.10.2015 / 10:15