Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite screws up image paths

         

Br3nn4n

10:37 am on Jul 29, 2009 (gmt 0)

10+ Year Member



Hey,

I'm using mod_rewrite to make urls like this:

www.blarg.com/articles/news/263

Well that works great! But then my images don't show since they're relatively linked.

I'm thinking I can put something in my images folder but I haven't the slightest idea WHAT...hahaha...

Any ideas?

Thanks!

jdMorgan

10:56 am on Jul 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> since they're relatively linked.

That is the problem, since it is the client (browser or search engine robot) that resolves relative URLs based on the URL it is currently looking at. Therefore, the 'base' URL for your example article is now "www.example.com/articles/news/", to which the relative image links are 'added'.

This will affect images, external CSS and JavaScript files, multimedia files --anything included as an object on your HTML pages-- as well as relative links to other pages outside that 'base' path.

The correct solution is to use server-relative or canonical URLs for image links. That is, start your image and other included-object links with a slash or with [<domain>...]

The other solution is to add a second rule (an internal rewrite) that removes or replaces the "/articles/news/" path from all included-object URLs. The problem with this is that it creates duplicate-content issues for all of those URLs. Also any objects shared between the "articles/news" URL-space and others on your site will be treated as separate objects from a caching viewpoint, potentially making your site far less efficient -- Remember that caching is based on an object's URL, not its "file location."

Jim

Br3nn4n

11:04 pm on Jul 29, 2009 (gmt 0)

10+ Year Member



Alright, so the simple solution is to just do like so:

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

Instead of:

<img src="image.jpg" />

?

Sorry to be honest that last paragraph is completely lost on me! I only just started using mod_rewrite from a tutorial last night and got the main part to work (/articles/news/###).

Thanks for your help!

Br3nn4n

11:42 pm on Jul 29, 2009 (gmt 0)

10+ Year Member



@My above post -- can't find any edit button?

Putting the slash (I tried it on one image so far) fixed it! :)

jdMorgan

12:29 am on Jul 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The answer is to put the full path from your "home page directory" to the image/script/whatever after the slash. This is still a 'relative path' but it's now relative from the domain, not from the current page URL.

Jim

g1smd

7:34 pm on Aug 1, 2009 (gmt 0)

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



From an organisational standpoint, you are often better off having a separate
/images/
folder on your server, rather than dumping many files into the site root.

I try to hold the minimum amount of files in the root: the .htaccess file, the index page, the robots.txt file, any SE account verification files, and so on.

Images, stylesheets, scripts, and so on, have their own separate folders.

Br3nn4n

9:16 pm on Aug 1, 2009 (gmt 0)

10+ Year Member



Yeah, I run a news site so all my images are in the /images/ folder, and then split into either /articles/ or /siteimages/

I totally agree with you on the matter :)