mod_rewrite and html paths

Discussion in 'Site & Server Administration' started by nicholasstephan, Jan 13, 2010.

  1. #1
    Hi,

    I see there are at least a few posts about this topic already, but I can't really find a solution...

    I've got a few RewriteRules in an .htaccess file, for example:

    RewriteRule ^portfolio/([0-9]+)$ project.php?gallery=$1
    RewriteRule ^portfolio$ gallery.php [NC]
    Code (markup):
    This works perfectly to redirect traffic and clean up the urls, but it breaks all the relative paths in the html; img/file.ext becomes portfolio/img/file.ext.

    The obvious answer to this is to make the paths absolute (/img/file.ext), but that makes development a mission as the site always has to be in the rood directory. If it's living on a testing server (www.mydomain.com/projects/client/version/img/file.ext) it all breaks, as you'd expect.

    So... what's the answer?

    Is there a way of telling apache that the root folder is "http://www.mydomain.com/projects/clients/version/"?? That way you can make all the paths absolute, fixing the url problem. And if I move the site to a new location I only have to change the one directive, fixing the portability problem?

    Thanks
     
    nicholasstephan, Jan 13, 2010 IP
  2. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #2
    The cure is to declare as the first line of your <head> the site base.
    I am using php so I have that defined in my configure file. In the head I use:
    In the config file $sitebase is defined as:
    I can't find the original post but you should be able to work it backwards from there.
     
    Colbyt, Jan 13, 2010 IP
  3. nicholasstephan

    nicholasstephan Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    base? I've never seen that! That's perfect! I've done (as a stopgap, hoping I'd figure it out) almost the same thing; defining a root php constant in my config file, but I've been putting a little pho echo in front of every URL! Not the most convenient way to do it. This makes it much easier.

    Thanks
     
    nicholasstephan, Jan 13, 2010 IP
  4. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #4
    It drove me crazy for years. I used 2 different menus and lots of other work arounds. Coulld not figure out what to search for in terms of the answer.

    A poster in another forum gave me the solution just a week ago.

    Happy to spread the cheer.
     
    Colbyt, Jan 14, 2010 IP