Can't include JS and CSS(htaccess problem)

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Postby Izzy » 04. January 2008 10:18

OK - sorry I am not understanding what you are trying to do by using a rewrite entry in the .htaccess file any way.

Are you trying to redirect any browser request for a file no matter what the URI in the browser is, to go to your index.php file only which includes links within the html code in the index.php file to other directories and files in your site tree that go to images, JavaScripts and css files - which the way you have it now excludes these files from displaying?

Try this:
RewriteEngine On
RewriteBase /mysite/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1{QUERY_STRING} [QSA,L]

This link has some variations on the theme:
Last edited by Izzy on 04. January 2008 11:20, edited 1 time in total.
Posts: 3344
Joined: 25. April 2006 17:06

Postby KallistaAEnvarou » 04. January 2008 11:14

You probably need to use headers, as I did.

header('Content-type: css');

For JS
header('Content-type: application/javascript');

Since you're using your index.php page to parse all of this stuff, you'll need to include a whole lot of if statements. You should write a function like this:

Code: Select all
function filename()
   Function Statement: takes $_SERVER['REQUEST_URI'] and strips from it the $_GET variable
         Server-defined superglobal variable
         Returns the page as requested in the URL minus anchor thingies (the ones that begin with #)
      $question=strstr($_SERVER['REQUEST_URI'],'?'); // returns everything after the first appearance of ?, if present
      $GLOBALS['filename']=substr($name,1); //quick way of removing the first / (versus eregi_replace("^/",'',$name), for example)
         makes $filename available in the main page

Now, keep in mind that, if you ever use <link rel="stylesheet" href="/csspage.css">, $_SERVER['REQUEST_URI'] becomes '/csspage.css' when that page is loading. The same idea goes for <script src="/javascriptpage.js"></script>. The same goes for include pages as far as I can tell. What this fact means is that, when you're writing your if statement in your index.php page, you'll need to accommodations, such as:

Code: Select all
   header('Content-type: css');
   echo file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/csspage.css'); //you don't really need $_SERVER['DOCUMENT_ROOT'], but I like to use it
   header('Content-type: application/javascript');
   echo file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/javascriptpage.js');

With the above function I wrote for you, all you'd need to do would be to call it with filename() in the files means that you won't need to utilise the $_GET[] variable since $_SERVER['REQUEST_URI'] will already hold the information $_GET[] would. $_SERVER['PHP_SELF'] will always be, for you, index.php, but, in this case, $_SERVER['REQUEST_URI'] is identical to $_GET['index']. With the function, you can accommodate for all $_GET[] requests in the URL bar. As well, you'll only need to write, in your .htaccess file:

Code: Select all
RewriteEngine On
RewriteRule .* index.php
Posts: 126
Joined: 02. December 2007 17:33
Location: Cold Cold California

Postby mgala » 04. January 2008 12:46

Sorry guys for the slow response!

I was reloading the page over and over again - didn't notice we had reached the page limit -_-


KallistaAEnvarou ->
I appreciate the your answer but the last solution by Izzy worked :P

Izzy ->
It works perfectly, I had to remove {QUERY_STRING} because $_GET['index'](^(.*)$ index.php?index=$1) was returning it's value plus "{QUERY_STRING}".

Thanks to both of you.

KallistaAEnvarou, I'm sorry you had to write all that(kinda my fault) :P
Posts: 9
Joined: 04. January 2008 07:17


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 62 guests