calexandru
calexandru

Reputation: 307

.htacces Appears only html with no CSS or PHP

I've been wondering on the internet on how to work with htacces(really hard to learn it). And when I was lurking in the internet, I found this: http://www.generateit.net/mod-rewrite/

Well, I inserted my url(working on localhost):
empresa.com/index.php?p=sub_artigo&id=1&cat=Mercearia

and it gave me this(with all options by default):
http://empresa.com/sub_artigo/5/Mercearia.html

And the .htacces code was this:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?p=$1&id=$2&cat=$3 [L]

And when I generate the url in php I do

<a href=\"sub_artigo/".$response2['ID_sub_artigo']."/".$response2['url'].".html\">$response2[nome_sub_artigo]</a>

And then, when I click the button, it appears like, only html. example: http://s14.postimg.org/wr137fx4x/htacces_error.jpg

Any idea what is happening ?

Upvotes: 0

Views: 65

Answers (3)

DaveRandom
DaveRandom

Reputation: 88697

Your PHP code is outputting relative paths to the style sheets.

Example:

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

Given your example input URL, this causes the browser to look for a stylesheet at the following URL:

http://empresa.com/sub_artigo/5/styles.css

This is because the browser doesn't know that the URL has been rewritten - it believes it is viewing a file in a subdirectory that doesn't really exist.

Instead you should use an absolute path, such as:

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

Notice the leading / on the path? This will tell the browser to look from the root of the domain:

http://empresa.com/styles.css

In this way you can still decouple your HTML from the protocol and domain/port (so you aren't tied to http://empresa.com) but the path will always be the same regardless of the URL that was used to reach the referencing page.

Upvotes: 1

jeroen
jeroen

Reputation: 91742

It looks like you are using relative links for your assets (images, javascript, css).

That means that when you look for css/my_stylesheet.css, from the new url, the browser will request a url like http://empresa.com/sub_artigo/5/css/my_stylesheet.css.

The easiest solution is to always use absolute urls for your assets, like /css/my_stylesheet.css, etc.

Upvotes: 4

Lucas Willems
Lucas Willems

Reputation: 7073

Try this code :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /empresa.com/index.php?p=$1&id=$2&cat=$3 [L]

Upvotes: 0

Related Questions