URL Rewrite issue to redirect - iis

I'm configuring a reverse proxy rule in my IIS, I want to redirect all traffic that arrive to my server on port 5050 to another server on port 8080 with regular expression, if I use (.*) the redirect works well, but if try to change it to for example 5050 the redirect does not work even if I test the patter and none errors are showed.
See my rule
enter image description here
Tested rule
enter image description here
Do you have any idea why this is happening?

In your Match URL, you use wildcards for matching, and the asterisk ("*") means "match any number of any characters and capture them in a back-reference" and "?" means match exactly one character (no back-reference is created), so {R:1} is invalid in Rewrite URL. I suggest you still use regular expressions to match.
More information about wildcards you can refer to this link: Rule pattern syntax.

Related

How to correctly rewrite URLs?

I am a real noob in IIS URL rewrite module.
I want to rewrite all requests of
127.0.0.1/Content/[anything may come here]
to
127.0.0.1:7078/Content/[anything may come here]
I am very bad in regular expressions and I do not know how to do this
I tried using the wildcard feature in URL Rewrite module and did this:
Requested URL matches the pattern using wildcards
127.0.0.1/Scripts/*
then rewrite to
127.0.0.1:7078/Scripts/*
(Action Type is Rewrite)
I am attached a screenshot. I am not sure I am doing this the right way, because it is not working.
1/ You don't need the host part in the Match URL Pattern : /Scripts/* should work.
2/ If you want to match the wildcard in the Match URL Pattern to the Rewrite URL, you'll have to use something like {R:0} in the Rewrite URL. For example 127.0.0.1:7078/Scripts/{R:0}.
3/ I'm not sure you can rewrite to a different port. If rewrites does not work, try Redirect as Action Type instead.
If you want to hide port 7078, you might need to use Application Request Routing (Reverse Proxy).
Sources : http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module & http://forums.iis.net/t/1165389.aspx?URL+Rewrite+to+specific+port

Does IIS / ARR rewrites one URL per line ONLY?

I have recently found out that, if there are multiple URLs (eligible for rewriting) in one line of a web page, the IIS / ARR would only rewrite the first match of that line, and ignore the rest. So I'd like to ask two questions:
Is this the default behavior of the IIS / ARR URL rewriting function?
Is there any work-around for this behavior, such that the IIS / ARR could recognize -- and rewrite -- multiple URLs on the same line?
The solution for me was to ensure Regex doesn't match anything outside of the quotes, i.e. stop it from being greedy.
Using the GUI the match pattern ends with ([^"]*) instead of (.*), and when saved in the Web.config this gets escaped to ([^"]*)
The line in my Web.config looks like this:
<match filterByTags="None" pattern="http://your.domain/~/media([^"]*)" />
So I think it's safe to assume you're referring to an outbound rewrite rule to change URLs in HTML responses.
No that is not the default behaviour of the rewrite module.
Update your question to include your rule configuration so that we can help.
Useful information:
In your <match> element if you do not specify the filterByTags, then the match pattern will be applied on the entire response content, regardless of lines and occurrences. Note that the evaluation of regular expression patterns on the entire response content is a CPU intensive operation and may affect the performance of the web application.
It sounds to be that your rule(s) aren't properly configured.
Further information:
URL Rewrite Module 2.0 Configuration Reference
Creating Outbound Rules for URL Rewrite Module

Need URL Rewrite to replace a string in a url

I am looking and looking and coming up with all different patterns, but no such luck. I require a rewrite rule to handle the a replacement of a string xyzlisting with xyz-listing. It may be anywhere in the url. Is there a straightforward way to write this rule?
I am not so experienced with IIS. However, I can suggest something:
In Apache mod_rewrite, you can use this rule:
RewriteRule (.*)(/\w+)(listing)(.*)$ $1$2-$3$4
This rule will split whatever string in the format anything followed by 'listing' that comes after mydomain.com/ with a '-'. Which results in 'anything-listing', wherever in the URL.
This will only change the last occurrence of this type of string. It will not replace more than one occurrence.
You can import this URl rewrite rule from Apache to IIS using this IIS extension:
IIS Extenson page for url rewrite module
Hope this helps :)

Url Rewrite Module Doesn't Redirect

I have set up an inbound rule in the IIS URL Rewrite module, but it is not working.
Here's my setup.
What I want is to say that if the user's browser navigates to a.b.com (the first rubbed out part - oh and of course it's not literally "a.b.com" but something else of that form) then he should be redirected to http://www.d.com/foo (the second rubbed out part).
I've used the the Test Pattern feature and it does match correctly.
You may have to set a condition to make it work.
Is it a wildcard, exact match, or regex pattern? That will also determine which things you will need to make it work
In your case you can just use the wildcard matching and it should work.

How to use htaccess to rewrite url to html anchor tag (#)

I have a situation where I want to take the following URL:
/1/john
and have it redirect using Apache's htaccess file to go to
/page.php?id=1&name=john#john
so that it goes to an html anchor with the name of john.
I've found a lot of reference to escaping special characters, and to adding the [NE] flag so that the redirect ignores the # sign, but these don't work. For example, adding [NE,R] means that the URL just appears in the browser address as the original: http://example.com/page.php?id=1&name=john#john.
This is possible using [NE] flag (noescape).
By default, special characters, such as & and ?, for example, will be converted to their hexcode equivalent. Using the [NE] flag prevents that from happening.
More info http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_ne
You can in fact do one of these things, but not both.
You can use the [NE] flag to signify to Apache not to escape the '#' character, but for the redirect to work, you have to specify an absolute URL to redirect to, not simply a relative page. Apache cannot do the scrolling of the window down to the anchor for you. But the browser will, if you redirect to an absolute URL.
What you want to do, can be accomplished with URL rewriting, or, more specifically, URL beautification.
I just quickly found this well explained blog post for you, I hope it can help you out with the learning to rewrite URLs-part.
As for the #-thing (expecting that you now know what I'm talking about), I don't see a problem in passing the same variable to the rewritten URL twice. Like: (notice the last part of the first line)
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$ /$1/$2/#$2 [R]
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&subpage=$2
Though, you'll have to escape the #-part, and it seems that it can be done this way:
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$ /$1/$2/\%23$2 [R,NE]
BTW, URL rewriting is not that hard (but can become complicated, and I'm not an expert), but Google can help a lot along the way.
You cannot do an internal redirect to an anchor. (Just think about it: how would Apache scroll down to the anchor?) Your link should pointo to /1/john#john. Anchors aren't part of the request uri.

Resources