我尝试这样做是......
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule \.js$ js.php [L] ## This line is the one I tried to add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
如果我使用标签<script type='text/javascript' src='http://www.domain.net/myfile.js'></script>
我希望它通过 js.php
.
js.php
将使用 header('....'); echo file_get_contents('myfile.js');
处理文件
我尝试了很多方法来做到这一点,我以前在一个项目中做过这个,但我不记得现在在哪里了,因为我必须重做:)
希望这是有道理的......
最佳答案
您可以添加以下重写规则:
RewriteRule ^([^\.]+)\.js$ js.php [NC,L]
代替:
RewriteRule \.js$ js.php [L] ## This line is the one I tried to add
结果是:
http://www.domain.net/myfile.js => http://www.domain.net/js.php
http://www.domain.net/foo/bar/myfile.js => http://www.domain.net/js.php
或者您可以执行以下操作:
RewriteRule ^([^\.]+)\.js$ $1.php [NC,L]
结果是:
http://www.domain.net/myfile.js => http://www.domain.net/myfile.php
http://www.domain.net/foo/bar/myfile.js => http://www.domain.net/foo/bar/myfile.php
关于使用RewriteRule flags的一些解释:
关于php - 使用 htaccess 将 Javascript 转换为 php 处理程序页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11295296/