views:

15

answers:

1

On the one hand mod_rewrite allows me to make userfriendly urls redirecting requests to proper script but on the other I can't use relative links because they will point at nonexistent files. A little example: mod_rewrite redirects request http://site.ru/param/one/page.html at http://site.ru/script.php while I have "myimg.jpeg" file at the same folder I can't use relative link <img src="myimg.jpeg /> cause in that case the browser will try to load img from the "http://site.ru/param/one/myimg.jpeg" address.

While using mod_rewrite what is the common practice to render right path to the img/css/js files?

I have 2 ideas about the realization: 1) to add base tag like that <base href="http://site.ru/" /> 2) to define a variable with the baseUrl and then use it while rendering src attributes like that: $baseUrl = 'http://site.ru/'; ... echo '<img src="' . $baseUrl . 'myimg.jpeg" />

Which is the best solution and are there any other ways to solve the problem?

+1  A: 

Don't use relative URLs. In almost all cases, a $baseUrl variable is unnecessary - just reference things by their path from the site's root.

i.e. <img src="/images/myimg.jpeg" /> instead of <img src="images/myimg.jpeg" />

ceejayoz
thank you. quite impressive solution. wonder how i missed that
actually I thought leading slash doesn't make sense