Friendly URL changing file link

Asked

Viewed 165 times

2

I have the following friendly url

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

However when I access meusite.com.br/news/14

It changes the links of the files to

noticias/css/principal.css

the original being

/css/principal.css

And when I access the link without the example friendly url

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

It works perfectly.

2 answers

2

You are currently inserting your CSS file in a relative way, that is, like this:

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

But in cases of URL friendly you should put the CSS (and other Assets like js and images) using an absolute path, which in case would be so:

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

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

1


your CSS must be with a relative path and will be interpreted from the last bar of the page address

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

So 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 get around the problem there are two solutions:

1- put the full address in the CSS

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

2- Use HTML widget <base>

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

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

Thus, all relative paths will not depend on the page address.

Browser other questions tagged

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