I have changed my website database, and i have more than 10000 url has been changed, and i need to do 301 redirect, what is best way to handle that big amount of data as 301 redirect ?
i cannt do it by using web.config, the file will became very big, and it will take much effort
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Permanent">
<add wildcard="/MyOldAspFile.aspx" destination="/MyNewFile.aspx" />
<add wildcard="/MyOldHtmlFile.html" destination="/MyNewFile.aspx" />
</httpRedirect>
so, what is the best way to handle that case ?
I want to recommend to use rewrite maps for that purpose. And store rewrite map in another file. In your web.config it will be:
<rewrite>
<rewriteMaps configSource="rewriteMaps.config"/>
<rules configSource="rewriteRules.config"/>
</rewrite>
In your rewriteRules.config;
<rules>
<rule name="Rule for Redirects">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
In your rewriteMaps.config:
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/old1" value="/new1" />
<add key="/old2" value="/new2" />
</rewriteMap>
</rewriteMaps>
And URL Rewrite Module should be installed in your IIS
You can fill rewriteMaps programmatically, if you have DB with old urls. Sample of simple logic is here:
var urls = new Dictionary<string, string>();
urls.Add("/old", "/new");
var lines = new List<string>();
lines.Add("<rewriteMaps>");
lines.Add("<rewriteMap name=\"Redirects\">");
foreach (var url in urls)
{
lines.Add(string.Format("<add key=\"{0}\" value=\"{1}\" />", url.Key, url.Value));
}
lines.Add("</rewriteMap>");
lines.Add("</rewriteMaps>");
System.IO.File.WriteAllLines(@"rewriteMaps.config", lines);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.