2016-09-30 2 views
0

앞에 내가 Global.asax에https로 리디렉션하고 www를 추가하는 방법 내가 HTTPS</p> <p>로 리디렉션하려고 링크

protected void Application_BeginRequest() 
{ 
    if (!Context.Request.IsSecureConnection) 
     Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:")); 
} 

의 코드 아래 사용하지만 내 문제는 내가 링크의 앞에 www를 추가해야한다는 입니다 즉 mywesite.se 및 HTTPS로 리디렉션 한 후에는 https://mywebsite.se을 좋아하지만 난 https://www.mywebsite.se

+1

예를 들어 주시겠습니까? –

+0

[URI와 경로 결합] 가능한 중복 (http://stackoverflow.com/questions/679171/combining-uris-and-paths) – Andrea

답변

0

당신은

void Application_BeginRequest(object sender, EventArgs e) 
{ 
    string lowerCaseURL = HttpContext.Current.Request.Url.ToString().ToLower(); 
    if (lowerCaseURL.IndexOf("http://dotnetfunda.com") >= 0) // >= because http starts from the 0 position :) 
    { 
     lowerCaseURL = lowerCaseURL.Replace("http://dotnetfunda.com", "https://www.dotnetfunda.com"); 

     HttpContext.Current.Response.StatusCode = 301; 
     HttpContext.Current.Response.AddHeader("location", lowerCaseURL); 
     HttpContext.Current.Response.End(); 
    } 
} 

에 yourwebsitedomainname.se

감사

dotnetfunda.com 교체 (web.config 파일에 쓰기) 이동
+0

impFunctions ?? –

+0

죄송합니다. 단지 함수를 대체합니다. 코드를 수정합니다. –

2

사용 UriBuilder처럼 원하는 :

var url = Context.Request.Url; 
var builder = new UriBuilder(url); 
builder.Scheme = "https"; 
if (!url.Host.StartsWith("www")) 
    builder.Host = "www." + url.Host; 

Response.Redirect(builder.Uri); 
,536,

면책 조항 :이 코드를 테스트하지 않았습니다. 여기

2
당신은 web.config에서 재 작성 규칙을 추가 할 수 있습니다

<rewrite> 
    <rules> 
     <clear /> 
     <rule name="Redirect non-www OR non-https to https://www"> 
      <match url=".*" /> 
      <conditions logicalGrouping="MatchAny"> 
       <add input="{HTTP_HOST}" pattern="^mywebsite.se$" /> 
       <add input="{HTTPS}" pattern="off" /> 
      </conditions> 
      <action type="Redirect" url="https://www.mywebsite.se/{R:0}" redirectType="Permanent"/> 
     </rule> 
    </rules> 
</rewrite> 
+0

그래,이게 더 나은 접근 방법 같아. – Andrey

+0

500 내부 오류가있다 –

+0

먼저 'Global.asax'로 작성한 것을 제거해야한다! –

관련 문제