I don't see why not. You'd just have to generate the SQL to insert the records. You may also have to restart the application pool afterward (in case Umbraco has any caching of the redirects).
While you could add hundreds or thousands of entries to the redirect table I wouldn't recommend it. Why? Performance.
Finding that 301 redirect is the last thing in the pipeline. Then the visitor is redirected and the page load finally happens. Granted, the lookup and pipeline isn't what you'd call slow. But it also isn't the best way to do this for bulk redirects.
Instead, the IIS URL Rewrite module is much, much better. First, it happens before the pipeline or Umbraco even gets involved. And it'll handle any amount of entries. And... you can use wildcards and regex instead of having to list every single entry.
FYI, redirects externalized using the configSource approach still count toward the size of the web.config, and so will still cause an error when the total size exceeds 250KB.
I actually avoid using redirects in the web.config. Once your web.config grows to about 250KB (I forget the exact number), you'll get a really obscure error that is hard to troubleshoot. The workaround is to edit the registry to increase this limit, but I find it best (for maintainability and to avoid surprises later) to simply avoid the issue in the first place by avoiding web.config redirects.
Performance of redirects shouldn't be a major issue in most cases. Ideally, search engines will pick up on the redirects and most users will eventually end up at the new URL's anyway.
Wildcards and regex only make sense if there is some pattern to the old and new names. This often happens when you've got a bunch of blog posts and you change from /about/blog/posts to /blog/posts. Or a product catalog changed from /products/a1-awesomizer/* to /products/awesome/* or similar.
If every 'before' and 'after' url is unique with no pattern then you'd need to add an entry for each one. This can be worthwhile for the performance gains. Or if performance isn't a huge deal or you really need content editors to see the redirects in the back office you could do it there.
I just wanted to mention the best practice, it's benefits, and why you can't do what you first asked directly in the back office.
But having said that... you might have reasons to do it anyway and that's cool. Just realize the options and pros/cons of each choice.
We've created a little snippet of code based on Nicholas' indications above (posted Jan 22, 2017 @ 23:30), to import our 500+ redirect entries created via the 301 URL Tracker.
The records seem to be added correctly into the umbracoRedirectUrl table,
these appear in the dashboard as well.
But the redirects are not working.
Are we missing something?
See below the code:
SqlDataReader tableUrlTracker = null;
try
{
myIRedirectUrl mode = new myIRedirectUrl();
SqlConnection DBConnToUMB = new SqlConnection();
DBConnToUMB = new SqlConnection(ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString);
DBConnToUMB.Open();
StringBuilder insertScript = new StringBuilder();
tableUrlTracker = new SqlCommand("SELECT * FROM icUrlTracker where is404 = 0", DBConnToUMB).ExecuteReader();
if (tableUrlTracker.HasRows)
{
while (tableUrlTracker.Read())
{
string oldUrl ="1928/"+ tableUrlTracker["OldUrl"];
DateTime createDateUtc =(System.DateTime)tableUrlTracker["Inserted"];
string nodeUniqueId = "";
int pageId=0;
if (tableUrlTracker["RedirectNodeId"] != null && !string.IsNullOrEmpty(tableUrlTracker["RedirectNodeId"].ToString() ))
{
int.TryParse(tableUrlTracker["RedirectNodeId"].ToString(), out pageId);
SqlDataReader tableUmbracoNode = new SqlCommand("SELECT top(1) * FROM umbracoNode where id ="+ pageId.ToString(), DBConnToUMB).ExecuteReader();
if (tableUmbracoNode.HasRows)
{
while (tableUmbracoNode.Read())
{
nodeUniqueId = tableUmbracoNode["uniqueID"].ToString();
continue;
}
}
tableUmbracoNode.Close();
}
if (pageId== 0)
{
continue;
}
string redirectUrl = "";
try
{
redirectUrl = new DynamicNode(pageId).Url;
if (nodeUniqueId == "" || redirectUrl == null)
{
continue;
}
}
catch
{
continue;
}
string encodedUrl = redirectUrl.ToSHA1();
string urlId = Guid.NewGuid().ToString();
insertScript.AppendLine("INSERT INTO [dbo].[umbracoRedirectUrl] ([id] ,[createDateUtc] ,[url] ,[contentKey] ,[urlHash]) VALUES('" + urlId + "','" + createDateUtc.ToString("yyyy-MM-dd HH:mm:ss") + "','" + oldUrl + "','" + nodeUniqueId + "','" + encodedUrl + "')").Append(Environment.NewLine);
insertScript.Append(Environment.NewLine);
}
}
insertScript.AppendLine();
}
catch(Exception e)
{
string s = e.Message;
}
finally
{
tableUrlTracker.Close();
}
Can I populate umbracoRedirectUrl table manually?
Can I populate umbracoRedirectUrl table manually?
I have 1200 content urls and I need to do 301 redirect for them.
Any help appreciated!
Thanks
I don't see why not. You'd just have to generate the SQL to insert the records. You may also have to restart the application pool afterward (in case Umbraco has any caching of the redirects).
Hi Thanks.
How to generate the urlHash and the id in the table?
It looks like you call the
ToSHA1()
function to generate the hash: https://github.com/umbraco/Umbraco-CMS/blob/5397f2c53acbdeb0805e1fe39fda938f571d295a/src/Umbraco.Core/Persistence/Repositories/RedirectUrlRepository.cs#L108For the ID, you can probably just create a new GUID with
Guid.NewGuid()
.FYI, to use the
ToSHA1()
extension method, you'll need to use the namespaceUmbraco.Core
: https://github.com/umbraco/Umbraco-CMS/blob/26a04ca7e91750a796c753f2f071cb1af8045cb1/src/Umbraco.Core/StringExtensions.cs#L739Thanks for the info. Appreciated!!
While you could add hundreds or thousands of entries to the redirect table I wouldn't recommend it. Why? Performance.
Finding that 301 redirect is the last thing in the pipeline. Then the visitor is redirected and the page load finally happens. Granted, the lookup and pipeline isn't what you'd call slow. But it also isn't the best way to do this for bulk redirects.
Instead, the IIS URL Rewrite module is much, much better. First, it happens before the pipeline or Umbraco even gets involved. And it'll handle any amount of entries. And... you can use wildcards and regex instead of having to list every single entry.
cheers,
doug.
Thanks for the reply.
What if the new urls are unique?
How will I create a regex or wild card for this, if I have to use url rewrite.
+1 for using IIS for redirects.
If you are worried about poluting your web.config is getting to big with all the redirects you can store them in a seperate config file.
This article explains how to do it : http://ruslany.net/2010/05/storing-url-rewrite-mappings-in-a-separate-file/
Dave
FYI, redirects externalized using the
configSource
approach still count toward the size of the web.config, and so will still cause an error when the total size exceeds 250KB.I actually avoid using redirects in the web.config. Once your web.config grows to about 250KB (I forget the exact number), you'll get a really obscure error that is hard to troubleshoot. The workaround is to edit the registry to increase this limit, but I find it best (for maintainability and to avoid surprises later) to simply avoid the issue in the first place by avoiding web.config redirects.
Performance of redirects shouldn't be a major issue in most cases. Ideally, search engines will pick up on the redirects and most users will eventually end up at the new URL's anyway.
Wildcards and regex only make sense if there is some pattern to the old and new names. This often happens when you've got a bunch of blog posts and you change from /about/blog/posts to /blog/posts. Or a product catalog changed from /products/a1-awesomizer/* to /products/awesome/* or similar.
If every 'before' and 'after' url is unique with no pattern then you'd need to add an entry for each one. This can be worthwhile for the performance gains. Or if performance isn't a huge deal or you really need content editors to see the redirects in the back office you could do it there.
I just wanted to mention the best practice, it's benefits, and why you can't do what you first asked directly in the back office.
But having said that... you might have reasons to do it anyway and that's cool. Just realize the options and pros/cons of each choice.
cheers,
doug.
Yes thats very true. Thanks for your opinion. Really appreciate that!
Hi all,
We've created a little snippet of code based on Nicholas' indications above (posted Jan 22, 2017 @ 23:30), to import our 500+ redirect entries created via the 301 URL Tracker.
The records seem to be added correctly into the umbracoRedirectUrl table, these appear in the dashboard as well.
But the redirects are not working.
Are we missing something?
See below the code:
Any ideas why these redirects are not working?
Thanks
is working on a reply...