How to Redirect any subdomain request to main domain in asp.net

。_饼干妹妹 提交于 2019-12-02 02:27:50

问题


I'm trying to redirect all subdomain requests for domain.com to www.domain.com and user must access all part of website only by enter to main domain and click on links , for example if user directly enter following address in browser:

subdomain.domain.com  

OR

www.domain.com/post/showPost.aspx?pid=11

i want to show error page and then redirect to www.domain.com

i use asp.net and IIS 7.5 and visual studio 2010

Thank you.


回答1:


Using the URL Rewriting module could be the way to go. Try using configuration similar to the following:

<system.webServer>
    <rewrite>
        <rules>
            <clear />
            <rule name="Redirect to WWW" enabled="true" stopProcessing="true">
                <match url=".*" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{HTTP_HOST}" pattern=".*" />
                </conditions>
                <action type="Redirect" url="http://www.domain.com/{R:0}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

This will redirect without showing an error page. If you want to show an error page then you'll need to redirect to a page that takes the return URL and does a client-side redirect to the right place.

As @inspile says you're going to have trouble doing it for sub-pages of the site. You may be able to do it using the referer to make sure it's from a link on the main site. Again the URL Rewrite module is the way to go here.

Cheers




回答2:


For the subdomains, you can either redirect them using your web hosting's Control Panel, or if you have the subdomains setup on a server that you can remote into, use the URL Rewrite module in IIS Manager to redirect all requests for the subdomain to your www subdomain. Using URL Rewrite, you don't have to show an error page, the redirect will happen automatically.

You're not going to be able to restrict users from accessing pages using links only.



来源:https://stackoverflow.com/questions/11420094/how-to-redirect-any-subdomain-request-to-main-domain-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!