开发者

mod_rewrite & file_get_contents problem

开发者 https://www.devze.com 2023-02-17 04:58 出处:网络
I\'m having this .htaccess file: Options +FollowSymLinks Options +Indexes RewriteEngine On RewriteBase \"/d/IT2 Tryggheim/it prosjekt 2011 - 2. termin/css/\"

I'm having this .htaccess file:

Options +FollowSymLinks  
Options +Indexes  
RewriteEngine On

RewriteBase "/d/IT2 Tryggheim/it prosjekt 2011 - 2. termin/css/"
RewriteRule ^css/(.*\.(css|less))$ rewrite.php?file=$1 [NC]

And inside the css folder I'm having an rewrite.php file that looks like:

#$file = $_GET['file'];
$file = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . $_GET['file'];

if(file_exists( $file ))
{
    $content = file_get_contents($file);
    # TODO: add code that parses the $content variable
    echo $content;
}
else
{
    echo "Weeeee! The file (<i>$file</i>) does not exist! :D <br /> <br />";
}

This is all tested on Wampserver. If I go to this path directly it works:

http://localhost/d/IT2%20Tryggheim/it%20prosjekt%202011%20-%202.%20termin/css/deafult.css

If I however have a file, one directory up which have the following HTML, in it, it doesn't work:

<link rel="stylesheet" type="text/css" href="css/deafult.css" />

Or, it doesn't work unless I disable the .htaccess, then it works. So how do I get the php to load the file?

Alternatives for $file that I have tried and which is not working开发者_开发技巧

$file = dirname($_SERVER['SCRIPT_FILENAME']) . '/css/' . $_GET['file'];
$file = '/css/' . $_GET['file'];
$file = 'css/' . $_GET['file'];
$file = './css/' . $_GET['file'];

Other stuff I tried

  • Tried using include instead of file_get_contents
  • Tried hardcoding in the filename ('deafult.css') instead of using the $_GET variable


Your rewriterule is looping.

Since you pass the name of the CSS file to your PHP script, you end up with something like:

.../css/rewrite.php?file=test.css <- it matches your rewriterule again.

It would be easier if you removed rewrite.php from the css folder


The PHP script doesn't get the directory css, so it is looking for /default.css instead of /css/default.css ;-)


I added this in the top of the rewrite.php file and now it works! :D

header('Content-type: text/css');
0

精彩评论

暂无评论...
验证码 换一张
取 消