301 redirect on IIS? - asp.net

i have tow websites e.g "testsite.ir" and "testsite.com". as i know my web site is on Windows Server 2008, Microsoft-IIS/7.5 .
how can i use 301 redirect to redirect users from .ir to .com?
can i use .htaccess file or i should use webconfig?
i use the code bellow in my webconfig, but nothing happen:
<rule name="Redirect from ir to com" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.testsite\.ir$" />
</conditions>
<action type="Redirect" url="http://www.testsite.com/{R:0}" />
</rule>
Any help would be appreciated!

If you have remote access to the server you can do this within IIS. You need to enable HTTP Redirect within IIS (add/remove windows features) before you can add a rule. Once you've enabled it you can redirect the .ir site to the .com site. See here: http://www.iis.net/configreference/system.webserver/httpredirect

Related

Redirect AzureWebsites www to non-www (certificate error)

I've tried many online solutions to get an azure site to redirect from www to non-www. The target site is successfully running as follows:
https://carterminal.online
I'm trying to get the www versions of the url to redirect to non-www. The last version of the rewrite urls I tried was the following (I also remember to clear the browser redirect cache):
<rule name="HTTPS and non-WWW only" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern="^www\." ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://carterminal.online/{R:1}" />
</rule>
Credit: Windows Azure: redirect website from www to non-www
However, when I insert the www in the browser, I'm getting a certificate error which refers to the azurewebsites certificate, not the custom one installed for carterminal.online.
I'm a bit confused as to why the wrong certificate is being consulted when using www - how can I remedy this?
You don't have the www version of your domain configured as a Custom Domain. Your SSL certificate does suppport the www version though, so that part is good.

IIS Redirecting non-WWW to WWW along with HTTP to HTTPS ISSUE

I want to redirect non-WWW to WWW along with HTTP to HTTPS in IIS 8.5. if users type http://example.com, http://www.example.com, or https://example.com, they all redirect to https://www.example.com.
This is my web.config file
<rewrite>
<rules>
<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite
Please note that in IIS i have ONLY 1 binding which is example.com, The problem is : for all the urls it redirects to https://www.example.com BUT the start page of IIS appears not my Default.aspx, and When i type https://www.example.com/Default.aspx it gives 404 Error.
You will need to bind each hostname to a website. Any hostnames not specifically bound to a website (or the servers IP address) will be handled by the Default Website in IIS (binding * - meaning any hostname not bound to a running Website).
A nice easy setup is to create 2 Websites in IIS (lets call them Application and Rewrites) - one to host your application, a second to handle rewriting other domains to your primary domain. Any domain you bind to the Rewrite Website will redirect its traffic to https://www.example.com.
###Application Website
Bind just your applications hostname www.example.com on port 443 (SSL only)
Deploy your application to this webroot
###Rewrite Website
Create a new Website in IIS, and create a new folder for its webroot.
Bind example.com on port 443 and port 80. Also bind www.example.com on port 80 only.
Create a web.config in the Rewrite Website webroot folder as follows:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect any traffic to Primary hostname" enabled="true" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Any traffic accessing http://example.com, https://example.com, or http://www.example.com will be redirected to https://www.example.com (including any path/query string)
NOTE: You could use the default website as the Rewrite website, or move the wildcard * binding from the default website to a new Rewrite website - to redirect any domain to your https://www.example.com - however this is bad practise / not advised.
Pre-requisite: To use this solution you'll need to install the IIS URL Rewrite extension if you don't have it installed already.

301 redirect in IIS

So we have a client whereby we are hosting their site but with 2 different domains mapped (we have bindings for both domains) to the same site in IIS. this is primarily because when they started with us they could only purchase the domain they didn't ultimately want to use going forwards.
For example the domains are www.some-thing.com and www.something.com.
They have asked if I can put a 301 permanent redirect in on the URL with the hyphen so then users should always go to the non hyphen version of the site.
I have tried URL rewrite rules and using the HTTP redirect in IIS however the site is then in a re-direct loop and I think this is down to the fact I have both domains bound to the site in IIS.
Help !!
Thanks in advance
Try this URL Rewrite .Here is the Source
<rule name="fromadd Host Name" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^anitkb\.com$" />
</conditions>
<action type="Redirect" url="To address" redirectType="Permanent" />
</rule>

DNS rewrite in asp.net or DNS redirection in asp.net

I have an aspx page with some HTML tags
Example
Actual url
<img src="https://google.com/fp/clear.png?latitude=<latitude>&longitude=<longitude> alt="">
How do i change the domain name (google.com) to a local URL (localgoog.com), and configure web server to redirect the URL (localgoo.com) to actual url (google.com)
Since you're referring to an aspx page, I'm assuming you're running on IIS. In that case, you may install the URLRewrite module into your IIS server. http://www.iis.net/downloads/microsoft/url-rewrite
Then, within IIS management console, you may setup a rewrite rule to redirect any url coming in with the hostname of "localgoo.com" to "google.com". Once you save this rule, your web.config file will be modified to include the XML version of your rewrite rule, which then makes your app portable to other servers as well.
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToGoogle" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^localgoo.com$" />
</conditions>
<action type="Redirect" url="//google.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
The {R:0} is a captured group of anything that comes after the hostname and appends it back onto the new hostname.

how to set asp.net default subdomain to www?

If have a website myws.com, if I request myws.com or www.myws.com I will see the same website
what I want is to be redirected to 'www.myws.com' when requesting 'myws.com'
Thanks
There are many ways to achieve a canonical redirect for the www subdomain, if you're using ASP.NET 4.0 you can do this:
if (!Request.Url.Host.ToUpper().Contains("WWW"))
{
Response.RedirectPermanent("http://www.myws.com/" + Request.Url.PathAndQuery, true);
}
You can also use the IIS URL Rewrite module, once you've installed it add this to your web.config file:
<rewrite>
<rules>
<rule name="WWW Canonical Redirect" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^myws.com$" />
</conditions>
<action type="Redirect" url="http://www.myws.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
If you've got access to IIS, you can use URL rewrite to permanently redirect traffic from myws.com to www.myws.com. Follow this tutorial: http://weblogs.asp.net/owscott/archive/2009/11/27/iis-url-rewrite-rewriting-non-www-to-www.aspx for URL rewrite.
If you don't have access to IIS, then you can add the following code into your global.asax.vb within the Application_BeginRequest event:
If Request.Url.Authority.ToString <> "www.myws.com" Then
Response.RedirectPermanent(Request.Url.Scheme.ToString() & "://www.myws.com" & Request.Url.AbsolutePath.ToString())
End If

Resources