I know this is a little offtopic from PHP but there's gotta be some apache gurus here.

My website is rather big and spans many many pages. All the pages are dynamically generated via php/mysql and are dumped in the home directory (no .php files in physical subdirectories).

Thus, all my images are called as such:

<img src="images/IMAGE_NAME.jpg">

(notice the lack of a header forward slash).

I'm OK at mod_rewrite, so Ive been changing my urls from like

myDomain.com/user.php?u=USERNAME => myDomain.com/users/username

to make things more coherent with a few rules.

HOWEVER, when the /users/username URL is maintained, the image paths do not work of course cause its looking for /users/username/images/IMAGE_NAME.jpg

how can I have a few rewriteCond and rewriteRules that will basically take all image requests from my own server to itself and rewrite the src to include http://www.myDomain.com/$1/$2(.jpg|.gif|.png)

Thanks... 😉

    20 days later

    Here is the rule I've used:

    RewriteRule !(images|css)/ index.php [NC,L]

    ... the important part is the : !(images|css)/

    which means do the rewrite for everything except calls to anything (even sub directories) within those directories. The "|" is the separator.

    Hope this points you in the right direction.

      i just set this up on my box, how it has an open dir of images and then loads them on a page with ads on the top. also for the high res pics it autoresizes if the width is more than 700 in width to fit

      here is my .htaccess

      RewriteRule Blog/(.) - [L]
      RewriteCond %{QUERY_STRING} !(.
      )auth=([0-9]+)$
      RewriteRule (.*).(jpg|jpeg|gif)$ /Blog/pic.php?pic=$1.$2 [NC,L]

      and the pic loader is in the /Blog dir

      here is the code

      
      insert header info, logo, etc
      
      <?
      
      if(!($pic=$_GET['pic']))
        die("Error: invalid image");
      $auth=mt_rand(11111,99999);
      
      ?>
      
      <img src='/<?=$pic?>?auth=<?=$auth?>' name='pic' onLoad='getorig(this);resize(this,scr)' onClick='resize2(this)'>
      
      
      
      <script>
      original_size=0;
      scr=screen.width-50;
      
      function getorig(img)
      {
        original_size=img.width;
      }
      
      function resize(img,s)
      {
      if(img.width>s)
      {
        img.height = parseInt(img.height * s / img.width);
        img.width = s;
      }
      }
      
      function resize2(img)
      {
      if(original_size>0)
      if(img.width>scr)
        s=scr;
      else
        s=original_size;
      
        img.height = parseInt(img.height * s / img.width);
        img.width = s;
      }
      }
      </script>
      
      
      
      

      hope that helps.

        Wordpress uses the following:

        <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
        </IfModule>
        

        What this does, is it rewrites all requests to /index.php, but ONLY IF THE FILE DOES NOT EXIST 🙂

        This is excellent, as it means that you can structure the content however you want, and static files still work, .php files still work, but other things (which don't physically exist in the filesystem), will be rewritten.

        If you're going to do this, be sure that you generate 404 responses correctly. Try not to make the script too runtime-expensive to run, as requests for nonexistent files will all hit it.

        Mark

          Write a Reply...