3

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?

4
  • Maybe this will help: stackoverflow.com/questions/13305963/… Commented Nov 11, 2012 at 18:31
  • 3
    Quick side-note: the code samples you posted create a potential security issue (path traversal). You should never trust user-input! Commented Nov 11, 2012 at 18:33
  • show us the line, on how are you including that test.css in your entry script. Commented Nov 11, 2012 at 18:52
  • To elaborate on Martijn's point: If you put that code on a publicly available web server you will open that server up to the whole world. You should instead compare the value of the URL parameter against a predefined set of values, and load the correct file based on that. For that to make sense the parameter should have a different name, say pageId, or something. Then do: if($_GET['pageId'] == 'page1') { require_once('path/to/file'); }. Commented Nov 11, 2012 at 19:05

2 Answers 2

1

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.

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

1 Comment

Yeah this was my problem. Fixed now! Thx :)
1

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" />

Comments

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.