Forum Moderators: phranque
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]
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]
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.
Jim
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):
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?
RewriteRule ^([^/]+)/?([^/]+)?/?(.+)?$ index.php?action=$1&modifier=$2&modifier2=$3 [NC]
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]
> 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]
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]
RewriteCond %{HTTP_HOST} ^development_server_name_or_IP$
RewriteCond $1 !^images/
RewriteRule ^([^.]+\.(gif¦jpe?g¦bmp¦ico))$ /images/$1 [L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(([^/])+)(/([^/]+)(/([^/]+))?)?/?$ index.php?action=$1&modifier=$3&modifier2=$5 [NC]