Forum Moderators: phranque

Message Too Old, No Replies

Rewrite rule gives URLs that look like non-existent subdirectories

Rewrite site.org/thing/33 => site.org/33 --browser wants imgs in /thing dir

         

splashsquelch

5:48 pm on Jan 15, 2008 (gmt 0)

10+ Year Member



Working in .htaccess I would like the following 'clean' style URL to redirect:

site.org/articles/33 => site.org/view.php?id=33

So I tried

RewriteEngine on
Options FollowSymLinks MultiViews ExecCGI
RewriteRule ^articles/([[:digits:]]+)$ view.php?id=$1 [NC]

but this confuses the browser. The redirect works (as in I get to view.php which has the $_GET argument), but the browser now looks for paths beginning site.org/articles, and so it cannot find the CSS, images, or any other actual files that do not reside in such non-existent directories.

My CSS resides in the same directory as view.php (public_html), but the images are (slightly wrongly) in public_html/images. So we have <img src="images/blah.png" /> for example, but <link rel="stylesheet" type="text/css" href="sitecss.css" />

Clearly I have misunderstood something in mod_rewrite, and would love to know what. My present solution seems rather a hack:

RewriteRule ^articles/([[:digit:]]+)$ /$1 [NC,R,L]
RewriteRule ^([[:digit:]]+)$ view.php?id=$1 [NC]

This works, but I feel it ought to be possible to avoid the external redirect, which removes the 'articles' from the URL. Live-with-able, but would be great to fix.

jdMorgan

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

WebmasterWorld Senior Member 10+ Year Member



Remember that it is the client (browser or robot) that resolves relative links. It does so by using the lowest-level directory path present in the page URL currently showing in its address bar. Therefore, the behaviour you see is expected.

There are several choices to avoid problems with link resolution:

  • Use server-relative links, instead of page-relative links -- e.g. <img scr="/logo.gif>
  • Use canonical links: <img src="http://example.com/logo.gif">
  • Rewrite requests for included objects back to the correct path (rewrite page-included objects only, not the pages themselves)

    Jim

  • g1smd

    12:49 am on Jan 16, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    You said you wanted a redirect, but the initial code is for a rewrite.

    It is a rewrite that you need just there.

    You also need a redirect in the reverse direction so that nothing can directly access the dynamic URLs.

    So:

    (www.)?domain.com/filename?parameter=value --> 301 redirect --> www.domain.com/value/value/

    /value/value/ --> internal rewrite --> /filename?parameter=value

    splashsquelch

    12:47 am on Jan 21, 2008 (gmt 0)

    10+ Year Member



    Ok, thank you both.

    phranque

    3:42 am on Jan 21, 2008 (gmt 0)

    WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



    welcome to WebmasterWorld [webmasterworld.com], splashsquelch!
    (after that long-time lurk!)