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:
http://wordpress.org/support/topic/140029
Last edited by Izzy on 04. January 2008 11:20, edited 1 time in total.
Izzy
 
Posts: 3344
Joined: 25. April 2006 17:06

Postby KallistaAEnvarou » 04. January 2008 11:14

You probably need to use headers, as I did.

For CSS
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
   Input
      $_SERVER['REQUEST_URI']
         Server-defined superglobal variable
         Returns the page as requested in the URL minus anchor thingies (the ones that begin with #)
   Process
      $question=strstr($_SERVER['REQUEST_URI'],'?'); // returns everything after the first appearance of ?, if present
      $name=str_replace($question,"",$localhost);
      $GLOBALS['filename']=substr($name,1); //quick way of removing the first / (versus eregi_replace("^/",'',$name), for example)
   Output
      $GLOBALS['filename']
         makes $filename available in the main page
   */
   $name=str_replace($question,"",$localhost);
   $filename=substr($name,1);
}


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
if($filename=='csspage.css')
{
   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
}
elseif($filename=='javascriptpage.js')
{
   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
KallistaAEnvarou
 
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 -_-

Anyway

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

Izzy ->
Thanks!
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
mgala
 
Posts: 9
Joined: 04. January 2008 07:17

Previous

Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 67 guests