regex redirect "/service" IIS 7 - regex

I am new to .NET and IIS ans I am trying do a regular expression redirect with this pattern \/([^\/]+)(?=\/[^\/]+\/?$) so my url wil go from mysite.com/en/solutions/exemple to mysite.com/en/exemple but in the redirection IIS put /Service in my URL someone as a idea how to fix this?
here is the rule generated in my web.config by IIS 7
<rewrite>
<rules>
<rule name="redirect salesforce" stopProcessing="true">
<match url="\/([^\/]+)(?=\/[^\/]+\/?$)" />
<action type="Redirect" url="/en/{R:0}/coveo-for-salesforce" appendQueryString="false" />
</rule>
</rules>
</rewrite>
Thanks
UPDATE:I tried
<rewrite>
<rules>
<rule name="remove solutions from url" stopProcessing="true">
<match url="^([^/]+)(/[^/]+)(/.*)" />
<action type="Redirect" url="/{R:0}{R:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I get a not not found
Please note that I am working on a Sitecore solution with 2 languages en and fr

If you want to remove the first level folder from the url, e.g.:
/en/solutions/example-page -> /en/example-page
/en/other-folder/another-page -> /en/another-page
/fr/one-more-folder/blah-page -> /fr/blah-page
with the 301 redirect code, use the following rule:
<rewrite>
<rules>
<rule name="remove solutions from url" stopProcessing="true">
<match url="^([^/]+)(/[^/]+)(/.*)" />
<action type="Redirect" url="/{R:1}{R:3}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
If you want to redirect with 301 code from mysite.com/en/solutions/exemple to mysite.com/en/exemple (so in fact remove solutions from all urls (but only if they are directly after language), you can use:
<rewrite>
<rules>
<rule name="remove solutions from url" stopProcessing="true">
<match url="^([^/]+)/solutions(/.*)" />
<action type="Redirect" url="/{R:1}{R:2}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I've tested the solution on Sitecore application and it definitely works.

Related

IIS URL Rewrite for Laravel Application Not working for multiple URI Segments

I am having a scenario that if URL doesn't contain anything in URI segment then no need to rewrite to index.php but if contains then rewrite it.
For Example: http://www.example.com/ or http://www.example.com No Rewrite Rule Required
But if URL is http://www.example.com/homepage or http://www.example.com/user/1 or http://www.example.com/edit-user/123/456
then it should be rewrite to index.php
I tried below.
<rules>
<rule name="custom rule 1" stopProcessing="true">
<match url="^([^/]+)/?$" />
<action type="Rewrite" url="index.php" />
</rule>
It is working for http://www.example.com/ and http://www.example.com/homepage but showing 404 for others pages.
The URI segments are dynamic and it can be multilingual.
Technologies used IIS, PHP (Laravel)
You can try this, it will match all kinds of URIs whether dynamic or multilingual.
<rule name="custom rule">
<match url=".*" />
<action type="Rewrite" url="index.php" />
<conditions>
<add input="{REQUEST_URI}" pattern=".*" />
</conditions>
</rule>
Test result
Fail request tracing
If this rule is only used for some URIs like user or edit-user, maybe this rule is more suitable.
<rule name="custom rule">
<match url=".*" />
<action type="Rewrite" url="index.php" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="user/(.*)" />
<add input="{REQUEST_URI}" pattern="edit-user/(.*)" />
</conditions>
</rule>

Building IIS Redirect Rule to Redirect Product Page to New URL Passing Product ID

I'm attempting to use the URL Rules engine in IIS 7 to redirect our current product pages to our new product pages.
Old Pages:
example.com/itemform.aspx?item=X12878
example.com/itemform.aspx?item=Y87304&showmenu=T
New URL
example.com/c/product/X12878
example.com/c/product/Y87304
Attempt
<rule name="Product Page Redirect">
<match url="/itemform\.aspx\?item=([.a-zA-Z0-9]+)$" />
<action type="Redirect" url="https://newsite.com/x/product/{C:1}" redirectType="Permanent" />
</rule>
Complete Rewrite Rule w/ Suggestion by Emma
<rewrite>
<rules>
<rule name="Product Page Redirect" stopProcessing="true">
<match url="/itemform\.aspx\?item=([A-Za-z0-9.]+)" />
<conditions>
</conditions>
<action type="Redirect" url="https://newsite.com/x/product/{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Accoridng to your description, if you want to use url rewrite to get the right query string.
More details, you could refer to below codes:
<rule name="QueryStringRue" enabled="true" stopProcessing="true">
<match url="itemform.aspx" />
<conditions>
<add input="{QUERY_STRING}" pattern="^item=([0-9a-zA-Z]*)" />
</conditions>
<action type="Redirect" url="http://example.com/c/product/{C:1}" appendQueryString="false" />
</rule>
Result:
My guess is that maybe removing the end $ anchor might simply make the match valid:
<rule name="Product Page Redirect">
<match url="/itemform\.aspx\?item=([A-Za-z0-9.]+)" />
<action type="Redirect" url="https://newsite.com/x/product/{C:1}" redirectType="Permanent" />
</rule>
but, maybe not.

Rewrite rule in web.config Umbraco

I like to use Umbraco with Vue.js and leave all the routing to Vue so I can make SPA. My goal is to write an IIS rewrite rule in web.config for using one Umbraco template for all routes except /umbraco. So far I have this
<rewrite>
<rules>
<rule name="Startview Templating">
<match url="^(http(s)?:\/\/)?(www\.)?([A-Za-z0-9:._-]*)(\/(?!umbraco)([A-Za-z0-9:_-]*))?" />
<action type="Rewrite" url="{R:0}?alttemplate=test" appendQueryString="true"/>
</rule>
</rules>
</rewrite>
This renders the same template (test)on every route I have so that works. But when I enter /umbraco I get a blank page. What am I missing?
Thanks
You should be able to add conditions to the rule to exclude paths, for example:
<conditions>
<add input="{REQUEST_URI}" pattern="^/umbraco/" negate="true" />
</conditions>
In your case i would look something like (I think you can simplify the match url though):
<rewrite>
<rules>
<rule name="Startview Templating">
<match url="^(http(s)?:\/\/)?(www\.)?([A-Za-z0-9:._-]*)(\/(?!umbraco)([A-Za-z0-9:_-]*))?" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/umbraco/" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}?alttemplate=test" appendQueryString="true"/>
</rule>
</rules>
</rewrite>

IIS rewrite rule not working in live environment

I have 4 servers in azure, 3 are load balanced and the 4th is for CMS purposes only.
SSL certificate has been added for the main website, but not for the sobdomain that the CMS is on.
I wrote a rule that should find any url that doesnt contain "backoffice" and match any other page to change it to https.
This works on regexr.com but for some reason doesnt work
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(https?:\/\/(?!backoffice).*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://www.WEBSITENAME.com{R:1}" />
</rule>
</rules>
</rewrite>
Url Rewriting 2.1 is installed on all 4 servers and i have created a load balance set in azure for https.
going to https manually works fine (along with loadbalancing).
Additional information:
I've tried many rules, including the existing answer. I can see things happening, like assets being brought in as https, but the page itself does not redirect.
There are 2 load balance sets, one for port 80 and the other for port 443. I don't know if this is corect, or could be a potential cause in the redirect not happening.
Your rule should be like that:
<rule name="http to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{REQUEST_URI}" pattern="/backoffice" negate="true" />
</conditions>
<action type="Redirect" url="https://www.WEBSITENAME.com{R:0}" />
</rule>
This rule will exclude requests with /backoffice path.
Also for issue of mixing content you need to fix your paths for css/js/images to relatives. Example:
<img src="/path/to/your/image.jpg"/>
Another way to fix mixed content is create outbound rule, which will change your output HTML (replace http: to https:):
<rewrite>
...
<outboundRules>
<rule name="Rewrite external references to use HTTPS" preCondition="IsHTML">
<match filterByTags="Script, Link, Img, CustomTags" customTags="HTML5Tags" pattern="^http://(.*)$" />
<action type="Rewrite" value="https://{R:1}" />
</rule>
<preConditions>
<preCondition name="IsHTML">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
<customTags>
<tags name="HTML5Tags">
<tag name="Video" attribute="src" />
</tags>
</customTags>
</outboundRules>
</rewrite>
Using the previous answer as a starting point, i made a few minor changes, to use HTTP_HOST rather than REQUEST_URI for the pattern negation and it works.
<system.webServer>
<rewrite xdt:Transform="InsertIfMissing">
<rules>
<rule name="http to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^backoffice\.WEBSITENAME\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.WEBSITENAME.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>

How redirect all web-site to other domain?

I have a test domain and it indexed in goolge, but it is wrong.
Now I need to create 301 redirect from all pages with all parameters to other domain.
I could to create action filter and create redirect on it, but it is wrong for me too.
I have known what it can implement in webconfig. But I don't understand how.
Do you help me please.
I try to use:
<rewrite>
<rules>
<rule name="Redirect to main subdomain from test domain">
<match url="^.*$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)uh261477\.ukrdomen\.com$" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="http://kitesurfing-base.com{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But it not work
You can place this rule in your web.config for this redirect:
<rule name="Redirect to main subdomain from test domain">
<match url="^.*$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)testdomain\.com$" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="http://otherdomain.com{R:0}" redirectType="Permanent" />
</rule>