Tommy
Tommy

Reputation: 697

mod_rewrite - absolute path of a folder

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..

Upvotes: 0

Views: 78

Answers (1)

IMSoP
IMSoP

Reputation: 97908

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';

Upvotes: 1

Related Questions