.htaccess path problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • foekall
    New Member
    • Jan 2007
    • 28

    .htaccess path problem

    dear all,

    I would like to ask about url rewriting in php. One of my project I got a problem
    with image path.

    .htacess
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^home/?$ index.php
    RewriteRule ^product/([0-9]+)/?$ product.php?prd _id=$1 [NC,L]
    I am using header.html as a common file. But the images from header.html only display in index.php. In product.htm none of image can't display. I think I have a problem in .htacess file. Please let me know about this.
  • rayckins
    New Member
    • Apr 2009
    • 1

    #2
    i think you are using relative link for images. Try using absolute path for images, hope u wil solve ur problem

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      You need to be very careful with images, and other resource paths, when using mod_rewrite.

      What you need to keep in mind is, the images are requested separately.
      So, if a user requests: http://example.com/home/
      And you have a rule: RewriteRule ^home/?$ index.php
      The request will be routed to: http://example.com/index.php

      However, any additional resources, like images, in that page will not be rerouted by that rule.
      Meaning, if that page includes an <img> at: images/header.jpg
      The request will be: http://example.com/home/images/header.jpg
      Which will not be rerouted by your original rewrite rule.

      If you want the image path to be re-written as well, you need to add a rewrite rule for that to. (Or come up with one that covers both)

      A simpler way, as rayckins suggested, would be to use absolute URLs for all resource links.
      Meaning: /images/header.jpg
      Rather than: images/header.jpg

      The added / to the link simply means that it should start at the web-root, rather than the current location.

      Comment

      • raamay
        New Member
        • Feb 2007
        • 107

        #4
        Originally posted by Atli
        Hi.

        You need to be very careful with images, and other resource paths, when using mod_rewrite.

        What you need to keep in mind is, the images are requested separately.
        So, if a user requests: http://example.com/home/
        And you have a rule: RewriteRule ^home/?$ index.php
        The request will be routed to: http://example.com/index.php

        However, any additional resources, like images, in that page will not be rerouted by that rule.
        Meaning, if that page includes an <img> at: images/header.jpg
        The request will be: http://example.com/home/images/header.jpg
        Which will not be rerouted by your original rewrite rule.

        If you want the image path to be re-written as well, you need to add a rewrite rule for that to. (Or come up with one that covers both)

        A simpler way, as rayckins suggested, would be to use absolute URLs for all resource links.
        Meaning: /images/header.jpg
        Rather than: images/header.jpg

        The added / to the link simply means that it should start at the web-root, rather than the current location.
        I am also with the same problem and has been wondering how to make it work. Well i am not sure about how to rewrite the image,css & js path in htaccess but i can say little about the absolute path you are referring to. Well, adding a slash in the infront doesnt solve the problem. Only if you use the entire long absolute path then it works something like href="http://localhost/mysite/images/image1.gif". And if we start using absolute path in all our hrefs i must say its not manageable for big sites.

        So, i feel there should be some way to use relative paths only. Or it can be done via the path rewriting method in htaccess. I tried this way but couldnt make it work href="<?php echo $_SERVERDOCUMEN T_ROOT."images/image1.gif"; ?>".

        Anyone can please tell us how to rewrite the path of these files aswell in htaccess.

        Comment

        • raamay
          New Member
          • Feb 2007
          • 107

          #5
          I found a solution for the path problem. I think you were using relative path for refering to image, css and js files.

          ok define a constant variable in your page something like
          Code:
          define("ROOT_DIRECTORY", "\your root directory\");
          to store the path of your root directory. Then whenever you want some image or css or js files to be used in your page you can do it in this way
          Code:
          href="<?php echo ROOT_DIRECTORY; ?>your image directory/image file"
          .

          But remember here that the constant ROOT_DIRECTORY should have a value of absolute path. If i am to give you an example, I am using WAMP, so i will give you the wamp example. My files are stored under the www directory of wamp i.e C:\wamp\www\mys ite folder. So now when i use absolute path i have to use \mysite\ as the root. Now, i will write my root_directory constant as
          Code:
          define("ROOT_DIRECTORY", "\mysite\");
          I have a image folder inside mysite folder. Now if i want to use a image i will reference it in this way
          Code:
          href="<?php echo ROOT_DIRECTORY; ?>image/image.gif"
          This will make sure that correct path is specified in any page whose url is rewritten.

          Hope this helps you. I also had the same problem as yours but i was able to solve it today following the above method.

          Comment

          • Mike Gertrudes

            #6
            Use <base href="http://www.yoursite.co m/yourfolder/" /> to fix this problem.

            Comment

            Working...