dongying3830 2014-02-16 14: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 14: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';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部