Results 1 to 3 of 3

Thread: htaccess Problem

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2005
    Posts
    54

    htaccess Problem

    I have the following code in my htaccess file:

    RewriteEngine on
    RewriteRule ^images/([0-9]+)/$ index.php?image=$1

    This is to convert URLs in the form:

    www.mysite.com/images/80/

    to:

    www.mysite.com/index.php?image=80

    This works, and the browser finds the correct page. However, in the HTML, all of the files that make up the page (CSS, JS and JPEG) are referred to by their filename only, as they are in the same directory as index.php.

    The problem is, that after redirection the browser is looking for the files in the wrong place, e.g. www.mysite.com/images/80/main.css instead of www.mysite.com/main.css.

    Anyone got any ideas why this is? Surely I don't have to link to every file and image with a full file path do I?

    Thanks.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: htaccess Problem

    this doesn't really have anything to do with PHP @_@.

    you just need to define an absolute file path for your stylesheets, or at least a relative path. you could build a relative path like ("../../main.css") when requesting those images, or you could specify an absolute path like ("http://mysite.com/main.css"). you can also just specify the top-most directory like ("/main.css"). I'm assuming that right now you're referring to your stylesheet via ("main.css" or "./main.css").

    if you don't understand, look at this example I've constructed below:
    Code:
    #current directory = http://mysite.com/
      <link href="main.css" type="text/css" /> #this refers to http://mysite.com/main.css
      <link href="/main.css" type="text/css" /> #this refers to http://mysite.com/main.css
    
    #current directory = http://mysite.com/images/80/
      <link href="main.css" type="text/css" /> #this refers to http://mysite.com/images/80/main.css
      <link href="/main.css" type="text/css" /> #this refers to http://mysite.com/main.css
      <link href="../../main.css" type="text/css" /> #this refers to http://mysite.com/main.css
    this is just basic file path manipulation. it will be the same for javascript references and images. you will need to specify the new path for everything like that.

    edit: oh, and I forgot to mention. the easiest and best way for you to avoid this is to simply refer to that stuff by going from your top directory (basically, you put a forward slash in front of all your links/references). instead of "main.css," you use "/main.css" instead. instead of a link going to "images/80/," your link needs to go to "/images/80/" instead. pretty simple.
    Last edited by kows; Mar 9th, 2007 at 03:10 PM.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: htaccess Problem

    Quote Originally Posted by snappel
    The problem is, that after redirection the browser is looking for the files in the wrong place, e.g. www.mysite.com/images/80/main.css instead of www.mysite.com/main.css.

    Anyone got any ideas why this is?
    Because the browser doesn't know about your internal redirection games. All it sees is the URL it sent to the server, and the relative URLs in the reply. These URLs are therefore interpreted as relative to the URL it sent to the server.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width