Working Directory when using URL rewriting

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
frozax
Forum Newbie
Posts: 4
Joined: Tue Jul 11, 2006 1:40 pm

Working Directory when using URL rewriting

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
frozax
Forum Newbie
Posts: 4
Joined: Tue Jul 11, 2006 1:40 pm

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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