ItsJohnB
ItsJohnB

Reputation: 71

htaccess and css files

I have the this project:

enter image description here

In the index I check the url with this code:

if( isset( $_GET['url'] ) ) {
   if( file_exists( 'classes/layout/'.$_GET['url'].'.php' ) ) {
    require_once 'classes/layout/'.$_GET['url'].'.php';
   } 
}

And in my .htacces this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*?)$ index.php?url=$1&%{QUERY_STRING} [L]

This works great. When is go to example: 127.0.0.1/test/pages/edit the index includes edit.php but in the index i use a css file named: test.css wich make the whole html background red.

If i go to 127.0.0.1/test/ i see the whole background red. But when i go to 127.0.0.1/test/pages/edit it is white. I checked the urls of the css file and i get this:

127.0.0.1/test/ = 127.0.0.1/test/cache/css/test.css

127.0.0.1/test/pages/edit = 127.0.0.1/test/pages/cache/css/test.css

Does someone know how to fix this?

Upvotes: 3

Views: 371

Answers (2)

Jon Lin
Jon Lin

Reputation: 143906

You need to either make the css links absolute (starts with a /test/) or add a base for all your relative links by adding this to your page header:

<base href="/test/">

(the base URI may need to be adjusted, but it looks like you want it to be /test/)

The reason this is happening is because the browser will guess what the URI-base is depending on the URL that's loaded. When you put http://127.0.0.1/test/ in the browser, the browser assumes the URI base is /test/ and all relative links will have that appended to the front. But when you put http://127.0.0.1/test/pages/edit in your browser, it assumes the base is /test/pages/ and thus your relative links get the wrong base appended to the front.

Upvotes: 1

bikash shrestha
bikash shrestha

Reputation: 429

The problem is that u did not give the proper path to the css. Always try to call style/script from base url as below: Hope this helps.

   <?php 
if($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
   define("SITEROOT","http://localhost/test/"); // base to your web directory ie,www or htdocs
    ?>
    //call style in this manner
    <link href="<?php echo SITEROOT; ?>Sourcefiles/cache/css/test.css" rel="stylesheet" type="text/css" />

Upvotes: 1

Related Questions