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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题