<补充-1> Url Rewrite && Url redirect

本文深入探讨了Nginx配置中的URL重写和重定向的区别,包括它们的工作原理、应用场景及对SEO的影响。通过解析301重定向和URL重写的处理流程,读者能更好地理解这两种技术如何在服务器端和浏览器端进行操作,以及如何在网站维护和优化中应用。
在配置Nginx时经常会看到url rewrite规则,这时就不得不区分redirect和rewrite的区别。

[b]1.URL Redirect (重定向)[/b]
通过[color=red][b]重定向[/b][/color],浏览器知道页面位置发生变化,从而[color=red][b]改变地址栏显示的地址[/b][/color]。
1) 通过重定向,搜索引擎意识到页面被移动了,从而更新搜索引擎索引,将原来失效的链接从搜索结果中移除。
2) 临时重定向(R=302)和永久重定向(R=301)都是亲搜索引擎的,是SEO(Search Engine Optimization)的重要技术。
3) Redirect是[b]浏览器和服务器发生两次请求[/b],也就是服务器命令客户端“去访问某个页面”
4) redirect的URL需要传送到客户端。
5) redirect是从一个地址跳转到另一个地址

比如[color=red]301重定向[/color]的处理流程:
1. User lands on original URL (用户用原始url登陆)
2. Using code, mode_rewrite, etc. the 301 redirects URL to the new one, literally changing the URL that is displayed in the browser from the old to the new (使用mode_rewrite等指令使原始url重定向到新的url,明显的变化就是浏览器上原始url变成了新的url)
3. URL Rewrite process begins again(接着就是url rewrite的流程了,这个流程后面介绍)
[img]http://dl2.iteye.com/upload/attachment/0103/5621/cb00a2dc-c5b7-345b-b760-27ec22d637ef.jpg[/img]

[b]2. URL Rewrite (url 重写)[/b]
[color=red][b]重写,不仅仅可以实现redirect在url上的重定向,还可以直接重写请求到实际的文件以及更多附加功能。[/b][/color]

1) rewrite的URL只是在服务器端
2) Rewrite规则是服务器内部的一个接管,在服务器内部告诉“某个页面请帮我处理这个用户的请求”,[color=red][b]浏览器和服务器只发生一次交互[/b][/color],浏览器不知道是该页面做的响应,浏览器只是向服务器发出一个请求。
3) URL重写用于将页面映射到[color=red]本站[/color]另一页面,若重写到[color=violet][b]另一网络主机(域名),则按重定向处理。[/b][/color]
4) rewrite是把一个地址重写成另一个地址。地址栏不跳转(若是重写到另一网络主机则也可能跳转)。相当于给另一个地址加了一个别名一样。

所以根据重写的地址跨域还是不跨域,rewrite可以做内部,也可以做外部的重写,rewrite可以让url变化,也可以让url不变化。

上述的例子就像用户去买手机,缺货时的两种处理:让用户自己去其他地方买(Redirect);公司从其他的地方调货(Rewrite)。

[b]3. rewirte和redirect对比[/b]
[img]http://dl2.iteye.com/upload/attachment/0103/5623/8369b043-4a09-335c-bfad-255d7b5299bd.png[/img]

参考资料:
[url]http://19920309.blog.51cto.com/8277196/1334449[/url]
[url]http://moz.com/blog/url-rewrites-and-301-redirects-how-does-it-all-work[/url]
[url]http://weblogs.asp.net/owscott/rewrite-vs-redirect-what-s-the-difference[/url]
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <connectionStrings> <add name="DB-NetShopsConnectionString" connectionString="Data Source=.\MSSQLSERVER2012;Initial Catalog=DB-NetShops;User ID=sa;Password=your_password;Integrated Security=False" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <!-- 添加全局cookie设置 --> <httpCookies httpOnlyCookies="true" /> <compilation debug="true" targetFramework="4.7.2" /> <httpRuntime targetFramework="4.7.2" maxRequestLength="20480" executionTimeout="300" requestValidationMode="4.7.2" /> <!-- 移除httpOnlyCookies属性 --> <sessionState mode="InProc" timeout="20" /> <authentication mode="Forms"> <!-- 移除httpOnlyCookies属性 --> <forms loginUrl="~/WebForm1.aspx" defaultUrl="~/WebForm2.aspx" timeout="20" /> </authentication> <globalization culture="zh-CN" uiCulture="zh-CN" requestEncoding="utf-8" responseEncoding="utf-8" /> <customErrors mode="RemoteOnly" defaultRedirect="~/Error.aspx"> <error statusCode="404" redirect="~/404.aspx" /> <error statusCode="500" redirect="~/500.aspx" /> </customErrors> <pages masterPageFile="~/Site.master"> <controls> <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </controls> </pages> </system.web> <system.webServer> <httpProtocol> <customHeaders> <add name="X-Content-Type-Options" value="nosniff" /> <add name="X-Frame-Options" value="SAMEORIGIN" /> <add name="X-Xss-Protection" value="1; mode=block" /> </customHeaders> </httpProtocol> <staticContent> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" /> </staticContent> <security> <requestFiltering> <requestLimits maxAllowedContentLength="20971520" /> </requestFiltering> </security> </system.webServer> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" /> </compilers> </system.codedom> </configuration> 添加母版页代码设置,生成完整webconfig代码
06-12
以下是网站目前的webconfig文件 改怎么修改 <?xml version="1.0" encoding="UTF-8"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=152368 --> <configuration> <connectionStrings> <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> </connectionStrings> <appSettings configProtectionProvider="RsaProtectedConfigurationProvider"> <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /> <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /> <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"> <KeyName>Rsa Key</KeyName> </KeyInfo> <CipherData> <CipherValue>G+DsdRtaxe0KW7q0OXT2A2TaUpeiZtkWsxvozK3E5gCFiP2u +nz7kdqzzC9be6ODsipCEoLTOKove4o1LZ34Vpgf7xUBBhZD15Zq8DdazCyYHnrn3TO5AMzjQ6I5f7NYspz69dw1kx6fI6yLHc8nwvXkxLq0aI1VZtTcc 525An4T7e6N4KOuNTnDQCUKO+EJoJh4W7tISZ37ygGQGzSH747CMGXWx9ZsVcNz+Jtc+ot +cGt9Gm/A6+b2NeQd3IgRe3qooXb6V0Jdi/2wONuPJ1FVWXfsnQRFD0imJxzFk86ChKKWK1c0SWMsrqZhUi0w4aTD26T +QL77Bg4qQsRGsQ==</CipherValue> </CipherData> </EncryptedKey> </KeyInfo> <CipherData> <CipherValue>O+M +/jfmUYbauWytN4pGYc55ElmhWj8SMkZUJmmwJ9Zzpm46To5tsbKWamxHvdqemCvIa9zUITppIFTuDdeejMVHHpPsXHUUkTk602Z3Uph94+nBSXVn0Agx DhHzgCEmRrv/1nFfffr6z6YleY1oSxjFDbyghteTiG5ksdw9bsbyLByvE6Kzx6D9lOwhN1U6m5nltBwFeYGJ +HKV5ayk3yKc9NkRf7nhzYzscKf9T8N74iNLNUddMiKojFSj4KZl+exXDj0vWG5emWBxwUStlx8ebXsxILBvHZUUE8xVmNWnue6MfGu369mB +ilqWmQivTsmPQIiW228rDPQtAfH74zAuQUftU1LlpcMeGQnCGc999FZQaBsScOwub0RUu2GB4OD562EzahxNGgzlRvt4ZiRcPDi32AVlHR5AKWn/ZpZi fZdQOUzvW/e8lrOSrJc0Yzl/piERRf4qmyYglnrC/U6+H8oJ0mcD6yfPtdoMLfLcuQpuMuvpaPrSbOPxQaPxfeq+eFc+ +q0GpTj0cFndrgdK0o7a0NGjKyrcuYIKgOG6nnL5E9IPUoVZp/yycJLSVb0QybgnKn0IBRVZZHt3p4wMMS9VyNlsLnl+DD7TaU=</CipherValue> </CipherData> </EncryptedData> </appSettings> <system.web> <httpRuntime enableVersionHeader="false" requestValidationMode="2.0" executionTimeout="500" maxRequestLength="409600" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" /> <customErrors allowNestedErrors="true" defaultRedirect="http://www.huadonghospital.com/index.html" mode="On" /> <compilation debug="false" targetFramework="4.0"> <assemblies> <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> </assemblies> </compilation> <authentication mode="Forms"> <forms loginUrl="~/Account/LogOn" timeout="2880" /> </authentication> <membership> <providers> <clear /> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> </providers> </membership> <profile> <providers> <clear /> <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" /> </providers> </profile> <roleManager enabled="false"> <providers> <clear /> <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /> <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /> </providers> </roleManager> <pages validateRequest="false"> <namespaces> <add namespace="System.Web.Helpers" /> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> <add namespace="System.Web.WebPages" /> </namespaces> </pages> <httpCookies httpOnlyCookies="true" /> <globalization requestEncoding="GB2312" responseEncoding="GB2312" uiCulture="zh-CN" culture="zh-CN" fileEncoding="GB2312" /> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <staticContent> <clientCache cacheControlMode="DisableCache" /> </staticContent> <defaultDocument> <files> <clear /> <add value="index.html" /> </files> </defaultDocument> <httpProtocol> <customHeaders> <remove name="X-Powered-By" /> <add name="X-Frame-Options" value="SAMEORIGIN" /> </customHeaders> </httpProtocol> <httpErrors errorMode="DetailedLocalOnly"> <remove statusCode="404" /> <error statusCode="404" path="/index.html" responseMode="ExecuteURL" /> </httpErrors> <security> <requestFiltering> <verbs> <add verb="OPTIONS" allowed="false" /> <add verb="TRACE" allowed="false" /> </verbs> </requestFiltering> </security> <rewrite> <rules> <rule name="HTTP to HTTPS redirect" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="off" ignoreCase="true" /> </conditions> <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" /> </rule> </rules> </rewrite> </system.webServer> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
最新发布
11-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值