views:

138

answers:

2

I need to rewrite all the files in a directory and essentially make their extentions css instead of php. I want to do with with a .htaccess file within that specific directory.

What is the correct method of doing so and are there any special PHP headers I need to set the ensure the file is read as a .css file correctly?

+4  A: 

I think that you rather want requests of *.css to be rewritten internaly to *.php. So try this:

RewriteEngine on
RewriteRule ^(foo/.*)\.css$ $1.php [L]

While foo is here the specific directory.

You now should explicitly declare the output as text/css using the following at the begin of the PHP script:

<?php
header('Content-Type: text/css;charset=<your charset>');

Replace <your charset> by whatever charset/encoding you are using (probably ISO-8859-1 or UTF-8).

Gumbo
lol, I didn't see that header mark when I wrote my answer. /me must be blind :P
Sekhat
The htaccess code wont work. I fixed the spelling mistake. Still get internal server error
Sam152
What error are you getting?
Gumbo
500: Internal Server Error
Sam152
And what does the server error log say?
Gumbo
Im using uniform server, there is a server log?
Sam152
There should be. Well without knowing the error we probably just can guess what the error might be. Have you tried an easier rule to see if mod_rewrite is working? Something like “RewriteRule ^ http://example.com/ [R]”.
Gumbo
[Fri Feb 27 23:06:57 2009] [alert] [client 127.0.0.1] Z:/www/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
Sam152
Oh dear, looks like mod_rewrite is not available on your Apache install. That's going to limit your possibilities a bit. You could still have URLs like “http://www.example.com/style.php/stylesheetname.css” by looking at PATH_INFO in the PHP script.
bobince
Im running it on my local machine, I could reinstall it or what not?
Sam152
Look into your httpd.conf file for a line that starts with “#LoadModule rewrite_module …”, remove the “#” and restart your Apache.
Gumbo
You win a million jelly beans.
Sam152
A: 

well they don't need a css extension if your just using them from with in your site. They just need to output the header Content-Type: text/css before rendering (I assume) the generated css. Similar sort of thing applies to other file types, if you have a php script that generates an image, you'd do it the same way, except you change the content type to image/png or jpeg or whatever.

However, if you want the outside would to see these generated css files as files with a css extension then mix what I've said with what Gumbo said.

Sekhat