Forum Moderators: coopster

Message Too Old, No Replies

mod rewrite and my problems

         

Majid

9:19 am on Aug 2, 2010 (gmt 0)

10+ Year Member



Hello,

I designed and wrote my website and everything is ok, I trying to change the URLs to virtual URLs with mod_rewrite, this is OK too.

for example my URL is:
h t t p://localhost/mysite/sections.php?section=news&do=detail&id=1

I changed to:
h t t p://localhost/mysite/news/detail/1

until this step everything is ok, my problem is :
my pictures on section.php, which not work.
because in src I wrote :
src="images/news_title.gif"
so when I use original URL the pictures will load fine but when I use virtual URL the picture cannot found because the browser want to load image from /news/detail/1/images/news_title.gif

now my question is:
is there any way to solve this problem ? should I use the website url on src of img tag? (I mean should I use img tag like this: src="h t t p://localhost/mysite/images/news_title.gif" )

I put <base> tag on top of my page and images loaded fine, but I have a bigger problem, all links tag (<a>) which have # in href will redirect to index page !

for example in original URL when I clicked on this tag :
<a href="#">Test</a>

the page will goes to top but in virtual URL when I click on above tag I will redirect to h t t p://localhost/mysite/#

would you please help me?



Regards

Matthew1980

9:42 am on Aug 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Majid,

Ok, the trick is with the src in images if you are using re_write rules (at least I think this is righ :)) is to use the <base ref> instruction in the <head> tags:-

<base href="http://www.yoursite.com/" />

then in your images:-

<img src="images/anImage.gif">

This will then use base ref as the reference point (note the trailing slash) and then uses that as a point of reference for the images.

So that treats the image src like this: http://www.yoursite.com/images/anImage.gif

At least that's how I understand it to function - and that's how I do what your trying to do.

Hope that helps.

Cheers,
MRb

Majid

11:04 am on Aug 2, 2010 (gmt 0)

10+ Year Member



Thanks Matthew for your reply,

your guide was very good but as I said in my previous post, when I use base tag I will have problem in my link tags (the page will redirect to main page when I clicked on a link which the href is "#" )

So I think I should use <base> tag but how can I solve this problem (I mean link problem) ?




Regards,

Matthew1980

12:01 pm on Aug 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Majid,

I haven't had this issue before, how are the links constructed? What happens in the status bar when you hover over the link?

I have just checked over my own to make sure I was correct, and I do exactly as I have advised, and where I have '#' in the href part, the link is effectively dead, unless it's something for javascript.

Cheers,
MRb

jdMorgan

12:51 pm on Aug 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem here is that it is the client (browser or search robot) which much resolve the relative URL-paths that it finds in links on your pages to the full canonical URLs that it must use to perform HTTP fetches from your server.

The client will resolve the page-relative URLs in your <a href="xyz">, <img src="xyz">, and <link href="xyz"> include tags by using the address that it 'sees' in its address bar as the base address.

Since you have changed the page URL seen by the client from http://localhost/mysite/sections.php to http://localhost/mysite/news/detail/1, the client now sees the two extra "directory levels" in that URL. Therefore, it will resolve your page-relative links using /news/detail/ as its base address, and the result will be that it tries to fetch the image from [localhost...] (The "/1" in your error report above should only occur if you added a trailing slash after "/1" in the URL that you request, or if you rewrite that address to add the trailing slash using another rewriterule).

The solution to this problem is to either use a canonical URL like <img src="http://localhost/mysite/images/logo.gif">, or to use a server-relative URL like <img src="/images/logo.gif"> -- Note that leading slash.

In the first case, the browser will completely ignore the address in its address bar, and load the image from the complete URL as given. In the second case, the browser will discard all of the path-info and use only the "domain name", appending "/images/logo.gif" to it, and therefore fetching the image from the correct address.

Your named anchors/URL-fragment problem can be similarly fixed by including the leading slash and referring to <a href="/#anchor"> instead of <a href="#anchor">.

Jim