0

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 ?

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Just a little thing. When I click different links, why do they accumulate in the url ?
@Sashka The url of the current page is used as the base url and as soon as you use a relative link, it is prepended to that url. If you use an absolute url, only the domain is prepended, just hover over any link on your page and check the complete url in the status bar of your browser.
1

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.

Comments

0

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]

3 Comments

Still the same, doesn't change.
It's extremely unlikely that that is the problem as the css file (and other assets) will not have the .html extension.
Yes it's your answer which right. Besides, I have often the same problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.