Forum Moderators: phranque

Message Too Old, No Replies

RewriteEngine relative path problem

RewriteEngine relative path problem

         

Francisc

7:39 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



Hello!

I am redirecting all 404 to index.php for my site in order to have Friendly URLs.

It works fine as far as that goes, however I have a problem with files (CSS, JS, images etc) that are linked to pages because when I have mysite.com/contact/ they don't load anymore because of Apache thinking Contact is a folder and thus relative paths don't work anymore.

How can I fix this please?
The only solution is to implement absolute paths or can I use an .htaccess trick?

Thank you!

jdMorgan

7:45 pm on Apr 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The recommended solution is to use server-relative paths, that is <img src="/images/logo.gif">, or full URLs (including "http:" etc.) instead of page-relative URLs such as <img src="images/logo.gif">

An alternate solution --and one that creates duplicate URLs, and therefore is not as 'clean'-- is to rewrite all included object URLs to strip the "fake" directory from the URL, and rewrite (not redirect) the request to the resulting filepath.

Jim

Francisc

8:50 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



Hm, thank you Jim. I will remember that regardless if I plan on using Friendly URLs or not as it seems a healthy practice. :)

To make sure:

It's
<img src="/folder/img.jpg"/>

- OR -

<img src="./folder/img.jpg"/>

Francisc

8:53 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



Hm, neither seems to work, it still wants ../

g1smd

9:05 pm on Apr 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I NEVER use ../ or ./ syntax.

I always start URL with / for 'root' folder.

Francisc

9:06 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



Hm, problem is it doesn't work for me like: /images/myImage.jpg.

Could it have something to do with Apache conf? I am testing on localhost.

jdMorgan

10:07 pm on Apr 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The /images/myImage.jpg URL-path specified in the link must be the correct and complete path from DocumentRoot to the image.

Neither "images/myImage.jpg," nor "./images/myImage.jpg," nor "../images/myImage.jpg" can be expected to work -- These are all page-relative forms.

If the 'real' server has Alias directives affecting some requests but not all, then we cannot predict their effect without knowing precisely what they are.

Jim

Francisc

10:16 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



Yup. I was using localhost/mySite.com/content.

In the real enviroment it will only be mySite.com/content and the "/" would suffice, however in my case "/mySite.com/" was needed.

Thanks a lot, Jim!

g1smd

10:52 pm on Apr 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you need to have the site both on localhost and on a live server, you can load the partial path into a variable and include that variable when building the href part of the links on the page. The logic that sets the path variable simply tests the requested domain name before making the decision. That's a simple fit it and forget it solution. The alternative would be to manually set that variable in your included config.php (or similar) file, and edit that file when moving it to the live site.

Francisc

11:01 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



Yup, I did that already in my variables.php file. :)
Thanks!