azure website rewrite rule in web.config to redirect .net to .com

Slee

I want to make sure all .net traffic goes to my .com so I created this rule but I am sure I am doing this wrong since it is not working:

 <rule name=".net to .com" enabled="true" stopProcessing="true">
        <match url=".*mywebsite.net.*"/>
        <action type="Redirect" url="https://mywebsite.com/" appendQueryString="false" redirectType="Permanent"  />
      </rule>

I assumed I had this regex written properly, but regex is not my strong point

David Faber

There is an issue with your regex BUT this is not generally how canonical domain name rules (which is what you want in this case) are set up. Generally you want to match every URL that comes in to your server and then filter the values of CGI environment variables (in this case, HTTP_HOST). Your rewrite rule would look something like this:

<rule name="CanonicalHostNameRule1" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^mywebsite\.com$" negate="true" />
    </conditions>
    <action type="Redirect" url="https://mywebsite.com/{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>

What this will do is forward all requests from URLs that aren't http://mywebsite.com or https://mywebsite.com to https://mywebsite.com. The actual URL is captured in the match and put in the parameter {R:1} - this way, if you go to say http://www.mywebsite.net/home.php, it will be redirected to https://mywebsite.com/home.php.

Also, if you want to enforce HTTPS, you can do the following:

<rule name="CanonicalHostNameRule1" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="^on$" negate="true" />
        <add input="{HTTP_HOST}" pattern="^mywebsite\.com$" negate="true" />
    </conditions>
    <action type="Redirect" url="https://mywebsite.com/{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>

The first condition will check if cgi.HTTPS is "on" ... and if it isn't, redirect to an HTTPS URL.

Note: If you want to redirect only mywebsite.net URLs, then the condition might look like this:

<add input="{HTTP_HOST}" pattern="mywebsite\.net$" />

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Redirect rule in web.config

From Dev

IIS url redirect rule in web.config

From Dev

"non-www to www" and "https" rewrite rule in web.config but not localhost ASP.NET MVC

From Dev

Simple rule(s) for IIS/Azure web.config file to redirect traffic to non-www HTTPS

From Dev

Simple rule(s) for IIS/Azure web.config file to redirect traffic to non-www HTTPS

From Dev

ASP.NET redirect non existing pages to homepage in web.config using rewrite rules

From Dev

.htaccess Redirect Rewrite Rule

From Dev

.htaccess Redirect Rewrite Rule

From Dev

Redirect and Rewrite Rule for Apache

From Dev

Redirect my website .com to .net

From Dev

Redirect my website .com to .net

From Dev

ImageResizer web.config rewrite rule not working properly

From Dev

301 redirect from one website to another using asp.net web.config file

From Dev

web.config rewrite to redirect html files but exclude certain folders

From Dev

How to create web.config URL Rewrite to redirect a subdomain to a pageroute

From Dev

web.config rewrite to redirect html files but exclude certain folders

From Dev

Nginx config rewrite rule combination

From Dev

htaccess - rewrite rule - redirect after url rewrite

From Dev

Azure web site - URL Rewrite using web.config not working

From Dev

Cannot config or rewrite routing in ASP.NET MVC Web Api

From Dev

Cannot config or rewrite routing in ASP.NET MVC Web Api

From Dev

Can't add rewrite rules to web.config .net core

From Dev

IIS rewrite rule to redirect https to http not working

From Dev

.htaccess rewrite rule redirect to 404 page

From Dev

Rewrite rule to redirect query to directory in .htaccess

From Dev

Rewrite Rule to Redirect index.php to www

From Dev

How to use a rewrite rule to redirect to https but relative?

From Dev

Rewrite rule for redirect url on different domain

From Dev

Rewrite rule to redirect query to directory in .htaccess

Related Related

  1. 1

    Redirect rule in web.config

  2. 2

    IIS url redirect rule in web.config

  3. 3

    "non-www to www" and "https" rewrite rule in web.config but not localhost ASP.NET MVC

  4. 4

    Simple rule(s) for IIS/Azure web.config file to redirect traffic to non-www HTTPS

  5. 5

    Simple rule(s) for IIS/Azure web.config file to redirect traffic to non-www HTTPS

  6. 6

    ASP.NET redirect non existing pages to homepage in web.config using rewrite rules

  7. 7

    .htaccess Redirect Rewrite Rule

  8. 8

    .htaccess Redirect Rewrite Rule

  9. 9

    Redirect and Rewrite Rule for Apache

  10. 10

    Redirect my website .com to .net

  11. 11

    Redirect my website .com to .net

  12. 12

    ImageResizer web.config rewrite rule not working properly

  13. 13

    301 redirect from one website to another using asp.net web.config file

  14. 14

    web.config rewrite to redirect html files but exclude certain folders

  15. 15

    How to create web.config URL Rewrite to redirect a subdomain to a pageroute

  16. 16

    web.config rewrite to redirect html files but exclude certain folders

  17. 17

    Nginx config rewrite rule combination

  18. 18

    htaccess - rewrite rule - redirect after url rewrite

  19. 19

    Azure web site - URL Rewrite using web.config not working

  20. 20

    Cannot config or rewrite routing in ASP.NET MVC Web Api

  21. 21

    Cannot config or rewrite routing in ASP.NET MVC Web Api

  22. 22

    Can't add rewrite rules to web.config .net core

  23. 23

    IIS rewrite rule to redirect https to http not working

  24. 24

    .htaccess rewrite rule redirect to 404 page

  25. 25

    Rewrite rule to redirect query to directory in .htaccess

  26. 26

    Rewrite Rule to Redirect index.php to www

  27. 27

    How to use a rewrite rule to redirect to https but relative?

  28. 28

    Rewrite rule for redirect url on different domain

  29. 29

    Rewrite rule to redirect query to directory in .htaccess

HotTag

Archive