I implemented some mod_rewrite magic to turn nasty id-filled urls into user-friendly ones with clear text names. Unfortunately, the paths for these new urls have no relation at all to the original path so all of my images, links, css, and javascript are all broken.

For example, the old url:
http://mydomain.com/dir/pg1.php?id=1234

new url:
http://mydomain.com/products/electronics/stereos/sony-walkman

Although the url is totally different, the HTML produced is identical. This causes a problem because all of my image, css, script, and links are RELATIVE references and I don't have any directory structures corresponding to /products/electronics/stereos.

There is no way at all to create all the necessary directory structures and put images in them because the directories differ for every product category and have no relation at all to my actual file structure.

Another possibility is changing relative file references to absolute ones (.e.g., instead of <img src="images/foo.jpg"> I could use <img src="/catalog/products/images/foo.jpg">. This will require A LOT of changes to my files and then I'll have to change everything all over again when I move the files from my test directory to the production directory.

Has anyone else dealt with this problem? I'd really like to know the easiest way to deal with it. Hopefully someone has a clever, elegant solution.

    Well yoou could mod rewrite the requested image paths to the actual paths.
    It is still donkey work but less donkey work than changing all the files.

    Use regular expressions to detect the directory and grab the file name which will then translate it to an absolute path in the mod rewrite stuff.
    The server should be calculating the file path to an absolute one before it gets to the mod rewite config so hopefully the rule will look more like
    RewriteRule catalog/product/images/(.*) /images/$1 [QSA,L]
    (My rewrite is a bit rusty)

    Relative paths have 2 benefits
    1. The site can be hosted off a sub folder of DOCUMENT_ROOT/HTTP_ROOT( good for promotional/client purposes )
    2. It is supposedley more machine efficient( does not have to go up all the folders, though that should never be an excuse for picking this option )

    Number 1 can be solved in future by using an HTTP_ROOT/DOCUMENT_ROOT constant that is calculated at the start. All urls/paths are then calculated off them to produce absolute paths.
    Ease of SEOing completely removes any warrant of number 2 being valid. If what is made is really that successful that it can bring a server down due to not using relative paths then themoney will be their for talks on sharding etc.

      You should use some kind of framework for that, To use absolute file paths instead of relative ones, Or at least have a conversion algorithm in your code to change your odd folder paths to the real ones.

        Thanks for the input (and happy father's day).

        It's way too late to use a framework. This is a legacy project I'm working with so there's no rewriting it in the time frame I'm facing.

        As for the rewriting, I'm not sure I can work that either. The relative path references vary in the directory depths which makes it really tricky to get a rewrite scheme together to reverse this one. Also, the directory at the 2nd level (i.e, domain.com/dir1/DIR2) completely varies. I would have to write some rewrite rule that
        1) doesn't conflict with the initial rewrite from the current filenames to the new SEO-friendly ones.
        2) doesn't conflict with mapping the seo-friendly urls back onto the actual PHP scripts -- the new ones that handle the seo-friendly urls.
        3) works at varying directory depth levels. e.g., sometimes we have:

        http://domain.com/category/other-category/product-id/

        and sometimes we have

        http://domain.com/category/other-category/product-id/session-id

        The rewrite situation is already becoming a bit tricky and I'm really reluctant to further complicate it. I've tried to solve this before using additional rewrite rules and something always seems to slip through the cracks and break.

        mpb001, I did originally use relative URLs so I could develop in a subfolder of my domain and simply move the code up a directory and have it work without having to edit all the files. This is precisely what seems to be biting me in the ass today.

        I've gotten a couple of other suggestions which seem pretty smart to me:
        1) Get another domain for development and put it in an entirely separate subdirectory. I can then develop there using absolute paths with abandon.
        2) use a <base> tag. Although I'm a bit worried about browser support for this, I think I'll give it a try.

          Write a Reply...