Forum Moderators: open

Message Too Old, No Replies

Broken links when rewriting a URL

Faking a directory structure is breaking relative links

         

keyter

10:40 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



I have used Application_BeginRequest() in global.asax to rewrite friendlier URLs (like www.example.com/one) to a single page, www.example.com/page.aspx?id=whatever.

This site has no directories; the "URLs" are just strings that are used to make it look like the site has folders and subfolders.

This works fine for URLS that have a single "directory".
www.example.com/one

However, a URL with another level, like:
www.example.com/one/two
will be rewritten to the correct page, except all relative links on the page are broken!

For example, if www.example.com/page.aspx has an image from ~/image.jpg, the URL rewriting from www.example.com/one/two will cause the link to become www.example.com/one/image.jpg and hence the link is broken, as are all other relative links.

I am using HttpContext.Current.RewritePath to rewrite the URLs.

Interestingly enough, this problem does not occur if the URLS are redirected (instead of rewritten) but redirecting defeats the purpose of friendly URLs.

[edited by: Xoc at 4:31 pm (utc) on Feb. 23, 2005]
[edit reason] changed to use example.com [/edit]

defanjos

10:57 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld

When I rewrite urls, I always make sure all paths are not relative.
Always use "http://www.example.com/image.jpg" instead of "/image.jpg"

[edited by: Xoc at 12:00 am (utc) on Feb. 10, 2005]
[edit reason] changed to use example.com [/edit]

keyter

1:00 am on Feb 10, 2005 (gmt 0)

10+ Year Member



The site using URL rewriting needs to be available under more than one domain, and needs to be used in a testing environment, under a separate application name.

[example.com...]
AND
[localhost...]

Is there a way to force relative links to be resolved using the physical file path, as opposed to the URL used in the browser?

tomasz

3:49 pm on Feb 10, 2005 (gmt 0)

10+ Year Member



Well, you could try <IMG SRC="http://www<%=Request.ServerVariables("SERVER_NAME")%>...

idonen

12:34 pm on Feb 16, 2005 (gmt 0)

10+ Year Member



Um... no no no...

Here's a way that actually works.

Use an absolute path like this:
"/whatever.aspx" whenever you link directly to something.

If your dev environment has an extra directory level above the root, then in the global.asax's application start event, either set a static class variable or an application variable based on what's in Request.ServerVariables["SERVER_NAME"]... eg:


if (Context.Request.ServerVariables["SERVER_NAME"] == "localhost")
Application["urlRoot"] = "/subdir";
else
Application["urlRoot"] = "";

and then in your aspx files, use:

"<%=Application["urlRoot"]%>/whatever.aspx" in your links. That way it will work on both your development and production servers.

carguy84

4:59 am on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



do what idonen says, start every link and every img src tag with ="/ and you won't have any trouble.

Your dev environment should mimic your live environment, otherwise your includes are going to be all screwed up too.

Chip-

Jalinder

7:32 am on Feb 19, 2005 (gmt 0)

10+ Year Member



Can the rewrite functionality be used with classic asp (.asp pages)? I have .net installed but the site is all .asp. I want to rewrite only categories but keep other files as .asp. Please give some suggestions on what's the best way to go about this.

idonen

7:36 pm on Feb 19, 2005 (gmt 0)

10+ Year Member



Yes, I've done it with classic .asp as well. The easiest way is to point 404 errors at one of your asp pages in IIS settings, and in the asp page you can examine and parse out the url from Request.ServerVariables("QUERY_STRING").

If it really is a 404, reset the headers so that the user gets a real 404, and if it's a valid category, send the content.

Jalinder

3:56 pm on Feb 22, 2005 (gmt 0)

10+ Year Member



if such pages return 404 search engines may not index the pages?

idonen

4:19 pm on Feb 22, 2005 (gmt 0)

10+ Year Member



They won't return a 404 if you re-write the header to return a 200 instead.

confused

7:38 am on Feb 23, 2005 (gmt 0)

10+ Year Member



hello everyone... do you feel that an absolute link is better than a relative link from an seo point of view, especially if the domain name is keyword heavy?

mattglet

1:12 pm on Feb 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



confused-

Your question does not belong in this forum, and is totally off-topic. Please use the Link Development [webmasterworld.com] forum for these types of questions.