Forum Moderators: phranque

Message Too Old, No Replies

Mod_rewrite is showing rewritten url

         

chadmg

5:07 pm on Dec 21, 2004 (gmt 0)

10+ Year Member



I'm a little stuck on this one. It is my understanding that when you perform a rewriterule it should not show the rewritten rule. Is this correct? Like if I have

RewriteRule ^blah/$ /yada/ [R]

The address bar should still show /blah/, right?

For some reason it's not doing this for me. Any clues on why?

jdMorgan

6:02 am on Dec 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



chadmg,

> ...Any clues on why?

Yes, because by using the [R] (redirect) flag, you have explicitly told mod_rewrite to do an external 302 redirect (which involves the client browser), so the browser will always update it's address bar before re-requesting the content from the new URL.

I believe you'll be happier with an purely-internal rewrite:


RewriteRule ^blah/$ /yada/ [b][L][/b]

See the mod_rewrite references in our charter [webmasterworld.com] for more info.

Jim

chadmg

2:12 pm on Dec 22, 2004 (gmt 0)

10+ Year Member



Thanks jdMorgan. I could use a little jd or some captain morgans. :)

One more question. If I have a non-absolute link on the page, say to "images/picture.png" in the yada folder. The rewriterule makes the page look for the image in the blah folder. Is there any way to process the local URI's on the page to use the actual location of the page?

WebsiteMgrs

3:22 pm on Dec 22, 2004 (gmt 0)

10+ Year Member



Since Jim just fixed this for me in another situation, it may work for you too:

RewriteRule \.(gif¦jpg¦png¦bmp)$ - [NC,L]

The dash '-' means not to do anything with the variables in the parenthesis and send as is.

Jim

jdMorgan

4:03 pm on Dec 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One more question. If I have a non-absolute link on the page, say to "images/picture.png" in the yada folder. The rewriterule makes the page look for the image in the blah folder. Is there any way to process the local URI's on the page to use the actual location of the page?

In order to avoid confusion and make thinking about these issues easier in the future, remember that it is the client browser that resolves relative URL-paths, not the server. So, the browser will resolve the on-page relative image URL-path relative to the current page's "directory level" and then issue a request for the image at that directory level. If that request then matches a rewriterule on the server, it will be rewritten.

So, in this case, you will find that you need to rewrite images *back* to the original directory, and avoid rewriting them on the front-end too.


# Rewrite non-image requests from /blah to /yada
RewriteCond %{REQUEST_URI} !\.(gif¦jpe?g¦png)$
RewriteRule ^blah/(.*)$ /yada/$1 [L]
#
# Rewrite browser-resolved relative image links back to original directory
RewriteRule ^yada/([^.]+\.(gif¦jpe?g¦png))$ /blah/$1 [L]

The first rule avoids rewriting any absolute image links you may have now or in future, while the second restores requests based on relative links to the proper path.

If you keep all of your images in a specific subdirectory, you can simplify the rules a bit, too:


# Rewrite non-image requests from /blah to /yada
RewriteCond %{REQUEST_URI} !/images
RewriteRule ^blah/(.*)$ /yada/$1 [L]
#
# Rewrite browser-resolved relative image links back to original top-level image directory
RewriteRule /images/(.*)$ /images/$1 [L]

Remember to replace all broken pipe "¦" characters with solid pipes if you cut and paste this code -- posting on this board replaces solid pipes with broken ones, and mod_rewrite requires solid pipes.

Jim