Page 1 of 1

Working Directory when using URL rewriting

Posted: Thu Jul 20, 2006 2:23 pm
by frozax
Hi all!

I'm using URL rewriting to change that:
http://www.example.com/word/an-other-word/yet-another
into:
http://www.example.com/index.php?var1=a ... et-another

The rewriting works good. The problem I have is with the working directory of my php file.
When I type directly http://www.example.com/index.php?var1=a ... et-another, I can include images files with relative links, such as "src=images/image.gif".
But when I type http://www.example.com/index.php?var1=a ... et-another, it's not working, I have to use absolute link (src="/images/image.gif") or a relative link such as "src=../../images/image.gif".
I'd prefer to use relative links, because it's handier to move a whole directory to a sub-directory on a server (a /beta/ to test it before releasing for example).
Is there a way to solve my problem?

Thanks a lot!

Posted: Thu Jul 20, 2006 3:41 pm
by RobertGonzalez
What I do is set a var that reads my root path, then I populate all of my src references to that so I always have an absolute path.

Posted: Thu Jul 20, 2006 4:37 pm
by frozax
Ok, that's what I was thinking about. So I suppose I have to use this workaround, and there is no simple solution... I'll have to change all my urls to images, css and local pages, then.
My pages will become quite ugly, I'll have to add lots of "<?php echo $dir; ?>" .... :?

Thanks Everah for the answer!

Posted: Thu Jul 20, 2006 10:07 pm
by RobertGonzalez
Let me get a little more specific. I usually create a function and a constant. The constant gets information from either a configuration file or from the settings in a database. It tells my script whether to use friendly or actual URLS. In the function, I pass to it the URL. The function then checks the URL type constant and returns the URL. Then I reference the function in my scripts.

Code: Select all

<a href="<?php print_url('aboutus'); ?>" title="Some Title">About Us</a>
This is an oversimplified example, but it is essentially what I do.