Clean URLs and Relative Paths

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Clean URLs and Relative Paths

Post by Christopher »

Clean URLs seem to have the side effect of making relative paths in images and links not work, because they use the clean URL as the base path.

I am looking for clean solutions for this. I have tried both adding a <base> tag with the true base path to the <head> and inserting the base path into every image/link with using templates. Both of these require templates. I am looking for a solution that will make code portable, so that you can put it in any subdirectory on a website and it just works.

Are there other solutions to this problem?
(#10850)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

So did I pick a topic with the perfect combination of obscurity and monotony? ;)

I am still searching. I looked into the HTTP headers Content-Location and Content-Base. Setting either of them did not have the same effect as the <base> tag in the header. Does anyone have experience using those headers?
(#10850)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

what's wrong with using relative path. I think this is the easiest way to be able to put the project in any directory.
for example:

Code: Select all

<img src="images/xoxo.png" />
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

jmut wrote:what's wrong with using relative path. I think this is the easiest way to be able to put the project in any directory.
Nothing and I prefer to do it that way. The problem comes when rewriting for clean URLs.

It is a little convoluted, but let me try to explain. Say you have a Front Controller script at "www.mysite.com/admin/index.php" and you want all your images in the "admin" area to be relative -- ok so far. But then you want clean URLs like "www.mysite.com/admin/users/list/". The rewrite rules run "/admin/index.php" and set PATH_INFO to "/users/list/", but because of the clean URL the base directory (for relative images and URLs) is set to "www.mysite.com/admin/users/list/" so none of the images in "www.mysite.com/admin/images/" referenced using "images/myimage.png" appear because the web server is looking in "www.mysite.com/admin/users/list/images/".
(#10850)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I often write the path in absolute (relative to the document root of course) .. Although you could handle a rewrite for images as well.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I've often had this problem, which at first made me avoid folders like the plague. I believe absolute is the only way to go (the leading slash). One thing to note is that if you plan on letting the application coexist with another on the same domain, by putting them in different folders, you'll have to make the part right before the leading slash configurable, i.e.

/Main_Page/Part_1 might be the link for a site only on that software, but
/wiki/Main_Page/Part_1 might be the link for a site that is using multiple software
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Unfortunately I need the code to be portable form development to staging to production URLs which can be almost any variation of different servers or directory paths. I have come down to putting a <base> tag in the header and having the Response object set the base URL for the site from the value in the current configuration:

Code: Select all

<base href="{BASE}"/>

becomes

<base href="http://www.mysite.com/path/to/site/"/>
I had hoped that adding one of the Content-* headers would work because that would have been transparent to any application code. Now the app needs to set the <base> tag somewhere.
(#10850)
Post Reply