0

I have this rewrite rule in web.config in my C# / .NET project:

<rule name="URL Rewrite Test" stopProcessing="true">
 <match url="(rewrite|testrewrite)-([0-9]+)/?" />
 <action type="Rewrite" url="./aspx/rewritetest.aspx?rewrite={R:2}" appendQueryString="true" />
</rule>

This works well with the following URL:

http://localhost/rewrite-123/

The rule is rewriting the traffic to:

http://localhost/aspx/rewritetest.aspx?rewrite=123

Problem example: http://localhost/rewrite-123/mess/up/relative/path/

The URL rewrite still works well when the link includes additional path segments. The problem is it includes the path segments with forward slashes in the relative paths, making relative URLs not work.

Page content consists of hundreds of relative paths. How would be the best way to solve this combination of URL-rewrite and relative paths?

rewritetest.aspx.cs:

public string relpath = "../pic/picture.gif";
Image1.ImageUrl = relpath;

rewritetest.aspx:

<asp:Image ID="Image1" runat="server" />

Translates to ../../../../../pic/picture.gif, and continues to function.

I believe it turn out like this:

http://localhost/rewrite-123/mess/up/relative/path/../../../../../pic/picture.gif
http://localhost/pic/picture.gif
<img src="../pic/picture.gif" />
<img src="<%=relpath %>" />

Path stays ../pic/picture.gif and stops working.

I believe it turn out like this:

http://localhost/rewrite-123/mess/up/relative/path/../pic/picture.gif
http://localhost/rewrite-123/mess/up/relative/pic/picture.gif
marc_s
759k185 gold badges1.4k silver badges1.5k bronze badges
asked Sep 1 at 12:56
2
  • You're comparing different things. Image1.ImageUrl is a property of a WebForms component, which gets resolved by the component itself before it's sent to the browser. It's not the same as the src attribute of the HTML element img, which gets appears in the browser as-is. Instead of assuming what the URLs look like, use your browser's Developer Tools to inspect the actual HTML elements and their attributes. Use the Network tab to see what calls are made to the server. Commented Sep 1 at 13:09
  • 2
    Finally, you can use the <base> to specify the base URL for all relative URLs in a page instead of using .. everywhere Commented Sep 1 at 13:10

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.