How to write a general rule to redirect all user urls to one valid Url by using asp.net mvc 5? - asp.net-mvc

I want to redirect if user type some Urls in the browser. My application is developed in Asp.net MVC 5.
please see the possible user inputs in the browsers
User can enter
1). test-systems.co.uk
2). www.test-systems.co.uk
3). https://test-systems.co.uk
4). https://www.test-systems.co.uk
5). http://www.test-systems.co.uk
All above url should be redirected to
https://www.test-systems.co.uk
I have tried the above rule but all URLs are not working as expected
This is my web.config rule detail
<rewrite>
<rules>
<clear />
<rule name="Rewrite subdomain" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" type="Pattern" pattern="^(www.)?\.test-systems.co.uk:4005$" />
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{URL}" pattern="(.*)" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="http to https"
enabled="true"
patternSyntax="Wildcard"
stopProcessing="true">
<match url="*" />
<conditions
logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off"
/>
</conditions>
<action type="Redirect"
url="https://{HTTP_HOST{REQUEST_URI}" redirectType="Found"
/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

IIS url rewrite dont do anything

I would like to url redirect from:
https://media.domain.com/link to file.ext
to:
https://media.domain.com:8080/link to file.ext
I have made this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
<directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
<rewrite>
<rules>
<rule name="Redirect to port 8080" enabled="true" stopProcessing="true">
<match url="(.*)/(.*)/(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
</conditions>
<action type="Redirect" url="http://{R:2}:8080/{R:3}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But when I enter https://media.domain.com/link to file.ext IIS just gives me a 404 error, and it does not redirect.
IIS simply ignores my url rewrite..
Any thoughts?
you could try below rule:
<rule name="domian rule" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^media.domain.com$" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="https://media.domain.com:8080/{R:1}" />
</rule>

Google not index website because of Http Header Redirect

hi webmasters i have been developed a web site with the asp.net mvc5 framework
www.esfandbar.com
i am new to SEO and i ask google to index my web site in google webmaster for about one month but google does not index that and total index is 0 when i inspect the problem with the http://www.siteliner.com i find out that except the home page all another page
skipped with the Http Header Redirect look at this photo please
all of my work for SEO is setting the rules in web.config file for:
redirect all the none www to www
redirect all the http request to https
remove the ending shash(/) from the Urls
how can i solve this problem? thank you for your time
this is redirection rule in web.config
<rewrite>
<rules>
<rule name="rule" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="[A-Z]" ignoreCase="false" />
</conditions>
<action type="Redirect" url="{ToLower:{URL}}" redirectType="Permanent" />
</rule>
<rule name="RemoveTrailingSlashRule1" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Rewrite domain requests" stopProcessing="true" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?([.a-zA-Z0-9]+)$" />
<add input="{HTTP_HOST}" pattern="^www\.esfandbar\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.esfandbar.com/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

IIS rewrite module canonical URL with two domains

I have a webserver and I have two domains with name http://example.mx and http://example.com.mx both are pointing to the same webserver and root folder, I can use my website with both domains and for the moment I can use www. as well but this is what I want to remove, I did it and I got this web.config file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<remove name="CGI-exe" />
<add name="CGI-exe" path="*.exe" verb="*" modules="CgiModule" resourceType="File" requireAccess="Script" allowPathInfo="true" />
</handlers>
<rewrite>
<rules>
<rule name="CanonicalHostNameRule2" enabled="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com\.mx$" negate="true" />
</conditions>
<action type="Redirect" url="http://example.com.mx/{R:1}" />
</rule>
<rule name="CanonicalHostNameRule1" enabled="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.mx$" negate="true" />
</conditions>
<action type="Redirect" url="http://example.mx/{R:1}" />
</rule>
<rule name="http a https" enabled="false" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But the problem is the I go to http://example.mx it sends me to http://example.com.mx and it suppose to works like this:
If I go to http://www.example.mx redirects to http://example.mx
If I go to http://www.example.com.mx redirects to http://example.com.mx
but right now if I go to http://www.example.mx it redirects to http://example.com.mx and I dont want this, What I need to change in the pattern?
You should use this rules:
<rule name="CanonicalHostNameRule2">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com\.mx$"/>
</conditions>
<action type="Redirect" url="http://example.com.mx/{R:1}" />
</rule>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.mx$" />
</conditions>
<action type="Redirect" url="http://example.mx/{R:1}" />
</rule>

IIS 8.5 rewrite: pass dynamic parameter

I am using the flowing rewrite map:
<rewrite>
<rewriteMaps>
<rewriteMap name="OldToNew">
<add key="/area.aspx?id=1" value="/area/1" />
<add key="/area.aspx?id=2" value="/area/2" />
<add key="/area.aspx?id=3" value="/area/3" />
<add key="/area.aspx?id=4" value="/area/4" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite rule1 for OldToNew">
<match url=".*" />
<conditions>
<add input="{OldToNew:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
Instead of writing each area.aspx page, I would like to transfer the page 'id' value as a parameter to the key value.
How can I do this?
Thanks.
rewritemap is used to associate 2 urls when there is no relation between urls. In your case, you could use regular expression matching.
The following rule should work
<rewrite>
<rules>
<rule name="Rule1">
<match url=".*" />
<conditions>
<add input="{REQUEST_URI}" pattern="/area.aspx?id=(.+)" />
</conditions>
<action type="Redirect" url="/area/{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>

IIS URL Rewrite if link does not match

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mywebsite.com$" />
</conditions>
<action type="Redirect" url="http://mywebsite.com/"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
That code works:
if it's website.com, it links you to another one
but, I want it so if it's NOT website.com, it links you

Resources