CRISHK Corporation
CRISHK Corporation

Reputation: 3008

How to redirect *.css to script.php file in HTACCESS? (Not index)

For example:

HANDLER *.css --> process-css-files.php
HANDLER *.(jpg|png|gif|jpeg) --> process-image-files.php

In addition how to:

if (*.css EXISTS) then
   include( THAT_FILE )
else
   process_URL-->( process-css-files.php )

Upvotes: 1

Views: 2653

Answers (3)

anubhava
anubhava

Reputation: 785176

I believe all this can be handled by .htaccess rules. Try something this:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteRule ^([^.]*\.(jpe?g|png|gif))$ process-image-files.php?img=$1 [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]*\.css)$ process-css-files.php?css=$1 [L,NC]

Upvotes: 0

sdleihssirhc
sdleihssirhc

Reputation: 42496

For the most part, you're going to use mod_rewrite to redirect URLs. You should put the following in your .htaccess file (if your server has support for it):

RewriteEngine on
RewriteRule ^/?(.+)\.css$ process-css-files.php?name=$1
RewriteRule ^/?(.+)\.(jpg|jpeg|gif|png)$ process-image-files.php?name=$1&extension=$2

That would solve the first part of your question (I think). I'm sure there's a way to get .htaccess to check if a file exists before rewriting the URL, but I'm more experienced with PHP, so I'd probably just always redirect to the PHP file, and then have PHP do all the checking and whatnot:

<?php
$name = $_GET['name'].'.css';
header('Content-type: text/css');
if (file_exists($name)) {
    echo file_get_contents($name);
} else  {
    // do whatever
}
?>

Upvotes: 1

robjmills
robjmills

Reputation: 18598

I use something like this for combining css scripts within the css folder to a single concatenated file:

RewriteRule ^css/(.*\.css) /combine.php?file=$1

Upvotes: 2

Related Questions