简体   繁体   English

将PHP文件用作具有CSS扩展名的CSS文件

[英]Using PHP Files As CSS File With CSS Extension

I've been doing some reading on using PHP files as CSS files. 我一直在阅读有关将PHP文件用作CSS文件的内容。 I understand how to do that, but I want to take it a step further if possible. 我知道该怎么做,但如果可能的话,我想更进一步。

Let's say I have a file called main-styles.php which contains my websites main style sheet. 假设我有一个名为main-styles.php的文件,其中包含我的网站的主样式表。

If I want to link to this file I would simply use <link rel="stylesheet href="http://example.com/css/main-styles.php"> . 如果要链接到该文件,则只需使用<link rel="stylesheet href="http://example.com/css/main-styles.php">

Simple enough, but what I am wanting to do is instead of having a .php extension I would like to have a .css extension instead but the file still be .php . 足够简单,但是我想做的不是使用.php扩展名,而是使用.css扩展名,但文件仍然是.php

Would this be possible through mod_rewrite or something? 通过mod_rewrite或其他方式可以实现吗?

I was thinking something like RewriteRule ^(.+)\\main-styles.php$ $1main-styles.css . 我在想类似RewriteRule ^(.+)\\main-styles.php$ $1main-styles.css

Would that be sufficient? 这样就足够了吗?

If I'm not mistaken that rule would not work. 如果我没记错的话,那条规则是行不通的。 I'm no expert in rewrite rules so bear with me. 我不是重写规则方面的专家,所以请多多包涵。

EDIT 编辑

I've tried the following in my .htaccess file: 我在.htaccess文件中尝试了以下操作:

RewriteEngine on RewriteBase / RewriteCond %{REQUEST_URI} !\\.(?:css|png|jpe?g|gif)$ [NC,OR] RewriteRule universal.css universal.php [L]

Solved see my own answer. 解决了,请看我自己的答案。

You can use the H or T flag of apach mode rewrite to force all files with php extension to be parsed by The css handler : 您可以使用apach模式重写的H或T标志来强制所有具有php扩展名的文件由css处理程序进行解析:

try the following in htaccess : 在htaccess中尝试以下操作:

RewriteEngine On
RewriteRule ^main-style\.php$ - [H=text/css]

or 要么

RewriteEngine On
RewriteRule ^main-style\.php$ - [T=text/css]

The rules above will change the mime type of main-style.php. 上面的规则将更改main-style.php的mime类型。

So I figured it out finally. 所以我终于想通了。 Turns out I had to list my directory. 原来我不得不列出我的目录。

Using the following in my .htaccess : 在我的.htaccess使用以下内容:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !\.(?:css|png|jpe?g|gif)$ [NC,OR]
RewriteRule css/universal.css css/universal.php [L]

I was able to keep my stylesheet a php file, but load it using a css extension. 我能够将样式表保留为php文件,但使用css扩展名加载它。

<link rel="stylesheet" href="http://example.com/css/universal.css">

Is handled as if it's 好像

<link rel="stylesheet" href="http://example.com/css/universal.php">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM