dongying3830 2014-02-16 22:15
浏览 38
已采纳

mod_rewrite - 文件夹的绝对路径

I'm using mod_rewrite, right now and i'm using this

RewriteRule ^.+(css/.+)$ $1 [L]
RewriteRule ^.+(js/.+)$ $1 [L]
RewriteRule ^.+(img/.+)$ $1 [L]
RewriteRule ^.+(fonts/.+)$ $1 [L]
RewriteRule ^.+(swf/.+)$ $1 [L]

Now i just write css/cssfile.css and js img and stuff and it works (even if i dont think this is the right way)

But anyways, i have a folder named functions with some PHP file, and i tried to make the same with that that i maked with this.. But that didnt go to great, the reason i want this is i want to make one file with includes, so i avoid, making several rows of code unnecessarily.

Does anyone knows what im talking about and maybe have a clue what i should do?

I'm sorry for bad explination (new to mod_rewrite) and i tried to search with out any good response..

  • 写回答

1条回答 默认 最新

  • dtpd58676 2014-02-16 22:28
    关注

    PHP's include statement does not resolve a URL or ask Apache for any information, so mod_rewrite will not affect its behaviour in any way.

    What you are looking for is a way to define the location of your files relative to the "document root" (the directory that maps to example.com/) rather than relative to the current file.

    For this you can use the PHP variable $_SERVER['DOCUMENT_ROOT'], e.g.

     include $_SERVER['DOCUMENT_ROOT'] . '/functions/connect_db.php';
    

    It might also be an idea to move the function definitions one directory above the document root, so that they can't ever be loaded as URLs in their own right. The function dirname() gives the "parent" directory of a particular path, so you might instead use:

     include dirname($_SERVER['DOCUMENT_ROOT']) . '/functions/connect_db.php';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决