homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Server relative paths and Apache
Or something like that...?

 7:53 pm on Jan 15, 2007 (gmt 0)


I've been using Apache in the WAMP package now for some time and am quite satisfied with it. Recently I discovered the magic treasures hidden deep within the file known as ".htaccess" and a little something called "RewriteRule". However, I've also run into some problems. Everything is working great. "localhost/my_page/something" translates into "localhost/my_page/index.php?variable1=something", and all images and stuff work. But when I add slashes to the mix, "localhost/my_page_something/", things gets fuzzy. After reading through the forums, I understand this has something to do with the paths of the images. So, I tried to make all image paths what I think is called "server relative paths", like "/images/image.jpg" instead of "images/image.jpg".

The problem with this is that it'll look for the "localhost/images/" map, not "localhost/my_page/images/", which is completely logical (or so I hope). My question to you is, is there some way to trick Apache into thinking that an image in "localhost/my_page/index.php" with the address of "/images/image.jpg" should point to "localhost/my_page/images/image.jpg" instead of "localhost/images/image.jpg"?

Obviously, this should all work without messing around like this when I put it online, with a domain name, on a separate server?

Perhaps there is even a simpler solution to my problem that my little brain hasn't yet managed to pick up? And sorry for the length of my post. It takes long time for me to ask something with (hopefully) a short answer. I also apologize if you find my English bad.

[edited by: Kaerigan at 7:55 pm (utc) on Jan. 15, 2007]



 10:12 pm on Jan 15, 2007 (gmt 0)

Your English is excellent --better than many native speakers-- I wouldn't have noticed anything if you hadn't said something about it.

You may have over-complicated the problem, which might be solved by a very slight change to your rule(s):

RewriteRule ^my_page_[^/]+[b]/?$[/b] /my_page/index.php?variable1=$1 [L]

This has little to do with server-relative versus page-relative linking. Rather, it has to do with the 'laws' of URL-paths: "/foo/mypage_something" refers to the file mypage_something in the subdirectory "/foo", while "/foo/mypage_something/" refers to the directory index or index page in the subdirectory /foo/mypage_something.

It sometimes helps when thinking about these issues to remember that it is the client (browser or robot) that resolves relative links. For a page-relative link (i.e. <img src="my_image">), the client will remove all information from the 'tail' of the current page's URL, with the 'tail' being defined as everything after the last slash in the page URL. It will then append the page-relative link.

For a server-relative link (i.e. <img src="/my_image">), the client will remove all path information from the current URL except for the domain name, and then append the server-relative link.

Remember also that the client is unaware of any internal rewriting that you may be doing, and this is the cause of many relative-linking problems. The solutions are to use server-relative linking or canonical (absolute-URL) linking, or to rewrite the 'incorrectly-resolved' requested URLs themselves.

Only page-relative links work well for developing a Web site on a PC without a server on it. But page-relative linking doesn't work well in some cases, for example, when trying to link to "/" or when dealing with ErrorDocuments.



 4:18 pm on Jan 16, 2007 (gmt 0)

Thank you for answering.

I (kind of) did what you said. First of all, shouldn't it be

RewriteRule ^my_page_([^/])+/?$ /my_page/index.php?variable1=$1 [L]

? And unfortunately, it didn't work. My RewriteRule was nearly already identical (the way I see it):

RewriteRule ^([^/]+)/?([^/]+)?/?(.+)?$ index.php?action=$1&modifier=$2&modifier2=$3 [NC]
Yes, I use two modifiers. And yes, the .htaccess lies in the same folder as index.php. To make things clear, everything lies in "c:/wamp/www/project". I don't really know what your code was supposed to do - I mean, doesn't it just allow URLs with a slash or without one instead of just one of those alternatives?

Perhaps after taking a look at my (real) code, you might find out what I've done wrong?

There is a kind of easy way out of this - I'll just need to change all image paths to server relative ones, but that way, I'll have to change it when I upload it to another server, since at that server, all files I'll be at the "root directory" (no, I don't really know what to call it). "/images/image.jpg"'ll point to what I want it to, which is not the case if I use it on Apache. I'm sorry if I misunderstood anything in your post.

[edited by: Kaerigan at 4:20 pm (utc) on Jan. 16, 2007]


 4:54 pm on Jan 16, 2007 (gmt 0)

Yes, sorry, I left out the parentheses need to create the $1 back-reference.

> I don't really know what your code was supposed to do - I mean, doesn't it just allow URLs with a slash or without one instead of just one of those alternatives?

Yes, it was meant only as an example. Remember, I don't know anything about your site, so that was only a general example of avoiding trailing-slash dependencies.

To avoid problems with missing or double slashes, you might want to include the slashes within each optional subpattern, like this:

RewriteRule ^([^/]+)(/([^/]+)(/([^/]+))?)?/?$ index.php?action=$1&modifier=$3&modifier2=$5 [NC]

The other option is to use multiple rules -- one rule for each possible combination of missing and present parameters.

To make your code portable between your development and on-line servers, you could always make an image directory rewrite rule dependent on the server name using a RewriteCond:

RewriteCond %{HTTP_HOST} ^web_server_name_or_IP$
RewriteRule ^images/([^.]+\.(gif¦jpe?g¦bmp¦ico))$ /$1 [L]

or the reverse:

RewriteCond %{HTTP_HOST} ^development_server_name_or_IP$
RewriteCond $1 !^images/
RewriteRule ^([^.]+\.(gif¦jpe?g¦bmp¦ico))$ /images/$1 [L]



 6:57 pm on Jan 16, 2007 (gmt 0)

Hmm... For some reason, I just cannot get it to work. So, I've kind of solved it for the moment. In my code there's the variable $path ( = "/project/", which is used when writing out addresses for the images and such. Perhaps it's time for me to put up my whole .htaccess file.

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(([^/])+)(/([^/]+)(/([^/]+))?)?/?$ index.php?action=$1&modifier=$3&modifier2=$5 [NC]

That is what in there right now (plus a few comments).


 7:59 pm on Jan 19, 2007 (gmt 0)

Well, I hate to double post, but I just thought that I'd tell you that I've solved it. At first the code in your last post didn't work - now it does. We just had to add a ".*/" before "images/...".



 2:44 am on Jan 20, 2007 (gmt 0)

Since you were talking about .htaccess in your first post, the code was intended for use in .htaccess. In order to work in httpd.conf or another server-level configuration file, the leading slash will be required, as you discovered.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved