Forum Moderators: phranque

Message Too Old, No Replies

RewriteBase failing in simple RewriteRule?

Is RewriteBase failing for me?

         

pha3z

7:31 am on Jan 27, 2006 (gmt 0)

10+ Year Member



I have this very simple test Rule:
RewriteBase /
RewriteRule ^t1/t2/$ showpicture.php?image=gallery/apic100.jpg [NC]

So it *should* do the following, right?
User Enters:
example.com/t1/t2/
Apache Redirects to:
example.com/showpicture.php?image=gallery/apic100.jpg

The page itself loads. But all the content on the page is broken. It acts like Apache is failing to set the RewriteBase to the root folder.

I'm lead to wonder because this alternative rule works fine:
RewriteBase /
RewriteRule ^t1_t2$ showpicture.php?image=gallery/apic100.jpg [NC]

where User Enters:
example.com/t1_t2
Apache Redirects to:
example.com/showpicture.php?image=gallery/apic100.jpg

But I don't want underscores in my URL! I want slashes for pseudo directories!

Can anyone tell me what's wrong?

[edited by: jdMorgan at 8:39 pm (utc) on Jan. 27, 2006]
[edit reason] Example.com [/edit]

jdMorgan

4:15 pm on Jan 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it is the browser you need to worry about.

The browser believes that the resource's URL is example.com/t1/t2/

It will therefore resolve any relative link it finds on pages for images and/or included scripts or stylesheets by using /t1/t2/ as it's 'current' directory loation, and resolving relative links based upon that URL-path.

There are two easy solutions. First, you can use canonical or server-relative links on your pages to locate images and scripts, e.g. <img src="http://www.example.com/images/widget.gif"> or <img src="/images/widget.gif">

Alternatively, you can implement rewriterules that also rewrite the resource requests from the relocated pages, e.g.


# rewrite pic requests to script
RewriteRule ^t1/t2/$ showpicture.php?image=gallery/apic100.jpg [NC]
# rewrite css requests to styles directory
RewriteRule ^t1/t2/(.+)\.css$ /styles/$1.css [NC]

This example may be a bit off-base, since I don't know precisely what you're doing or the exact problem with resolving the URLs, but the key is that it is the client (browser or 'bot) that resolves relative URLs, and that is where the problem is actually occurring, because the URL has been rewritten, the /t2 directory level doesn't actually exist, and the browser is trying to resolve links relative to the /t1/t2/ directory level.

Also, while testing, I'd suggest you server-anchor the substitution URL as appropriate. For example:


RewriteRule ^t1/t2/$ [b]/sh[/b]owpicture.php?image=gallery/apic100.jpg [NC]

You can find out what URL-path is failing by looking at your raw server error logs after loading a page with broken images, etc. on it. It will show the failed request and the filepath resolved from the URL. That's often a good clue on how to fix the problem.

Jim

pha3z

7:57 pm on Jan 27, 2006 (gmt 0)

10+ Year Member



Thanks, Jim! Great Explanation!

My site navigates beautifully, now!
Have a look:

<snip>

[edited by: jdMorgan at 8:39 pm (utc) on Jan. 27, 2006]
[edit reason] Sorry, no URLs, please. See TOS. [/edit]