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 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0