I am looking to change these URLs to the following format without rearranging the actual nodes or doing manual per-page URL changes, presumably using URL rewriting:
In this scenario, it's safe to assume that the word "translations" and the language abbreviations won't be used elsewhere in the URLs.
Unfortunately, though, I am not familiar with regular expressions. If someone could show me how to do this, or at least point me in the right direction, I'd really appreciate it.
The example uses regex to match the incoming URLs. It assumes that both path and subpath only consists of alphanumeric characters and/or hyphens.
It creates a permanent redirect to the "new" URL. If the user should keep seeing the "old" URL, you can change the type parameter to Rewrite instead of Redirect.
This looks like it would only work for URLs that have exactly two levels, though. How could it be modified so it will work with any number of path levels before "/translations"?
In other words, it would help if it could transform:
So I have a real page on Umbraco whose native URL follows this format:
www.example.com/path/subpath/translations/es/
I added the second rule you provided to the Web.config file, and entered in the URL below:
www.example.com/es/path/subpath/
But I just get a 404 message. Any idea why this might be? Looking at the rule, it seems like the "/translations/" might be on the wrong side of the rule.
Just out of interest... are you expecting the url in the browser to change or are you want it to display the content from that page?
If it's the latter, you might be wanting something a bit more complex than a re-direct type rule you are probably looking more a rewrite rule or even a custom contentfinder.
Thanks so much Anders! The latest code you provided seems to work great, and the "Rewrite" attribute does just what I needed!
I realized a complication, though. This will work great when the URL is typed in, but when Umbraco is outputting URLs via Razor or the APIs, it is still going to use the original URL.
How could I have the URL that Umbraco stores natively also follow the new format? Would that require the use of a content finder?
A content finder is for handling incoming URLs. Like Nik mentioned, you could have used an content finder instead of URL rewriting.
To give a content item another URL, you can instead use a URL provider. This will let you control what the Url property returns. There's a bit documentation about URL providers here:
Keep in mind that URL providers only provides the visual URL. To make sure that Umbraco actually finds the correct content, you could use a content finder. Or just the URL rewriting like we've discussed in this thread.
I'll try making a URL provider to go along with the URL rewrite rule, and if that doesn't work, I'll try switching out the URL rewrite for a content finder.
Help with URL Rewriting?
Hi,
I have a site that has URLs in the following format:
I am looking to change these URLs to the following format without rearranging the actual nodes or doing manual per-page URL changes, presumably using URL rewriting:
In this scenario, it's safe to assume that the word "translations" and the language abbreviations won't be used elsewhere in the URLs.
Unfortunately, though, I am not familiar with regular expressions. If someone could show me how to do this, or at least point me in the right direction, I'd really appreciate it.
Thanks, David
Try something like this:
The example uses regex to match the incoming URLs. It assumes that both
path
andsubpath
only consists of alphanumeric characters and/or hyphens.It creates a permanent redirect to the "new" URL. If the user should keep seeing the "old" URL, you can change the
type
parameter toRewrite
instead ofRedirect
.Hope that helps ;)
Hi Anders,
Thanks so much for the reply.
This looks like it would only work for URLs that have exactly two levels, though. How could it be modified so it will work with any number of path levels before "/translations"?
In other words, it would help if it could transform:
to:
Any ideas?
Thanks,
David
Yes. I wasn't sure whether you needed that. We can make the regex less specific:
It works with your example ;)
Hi Anders,
Thanks again for your help so far.
So I have a real page on Umbraco whose native URL follows this format:
I added the second rule you provided to the Web.config file, and entered in the URL below:
But I just get a 404 message. Any idea why this might be? Looking at the rule, it seems like the "/translations/" might be on the wrong side of the rule.
Thanks,
David
Just to be sure, what address do you enter in the location bar, and what address do you see in the location bar when you get the 404?
I've tried to test up a test solution with the rule I posted before, and then point my browser to:
I'm then immediately redirected to:
A page must exist with this URL - otherwise you'll get the 404 page.
Hi Anders,
Sorry for the confusion! I looked back and my original post was vague. I'm looking for it to work the other way around.
I want to be able to enter in something like this to the URL bar:
...and have it arrive at the Umbraco page with the default URL of:
Is there any way this can be done? Really appreciate your help!
Regards,
David
Should be possible as well - try something like:
Hi David,
Just out of interest... are you expecting the url in the browser to change or are you want it to display the content from that page?
If it's the latter, you might be wanting something a bit more complex than a re-direct type rule you are probably looking more a rewrite rule or even a custom contentfinder.
Nik
Yes, as I wrote earlier, the
type="Redirect"
attribute can be changed totype="Rewrite"
, and the URL in the location bar won't change.A content finder becomes slightly more complex, so I would stick with URL rewriting if possible.
Ahh, I missed that in your earlier post Anders :-)
Thanks so much Anders! The latest code you provided seems to work great, and the "Rewrite" attribute does just what I needed!
I realized a complication, though. This will work great when the URL is typed in, but when Umbraco is outputting URLs via Razor or the APIs, it is still going to use the original URL.
How could I have the URL that Umbraco stores natively also follow the new format? Would that require the use of a content finder?
Thanks,
David
A content finder is for handling incoming URLs. Like Nik mentioned, you could have used an content finder instead of URL rewriting.
To give a content item another URL, you can instead use a URL provider. This will let you control what the
Url
property returns. There's a bit documentation about URL providers here:https://our.umbraco.org/Documentation/Reference/Routing/Request-Pipeline/outbound-pipeline#urls
Jeroen Breuer also wrote an article 24 days of Umbraco about URL providers:
https://24days.in/umbraco-cms/2014/urlprovider-and-contentfinder/
Keep in mind that URL providers only provides the visual URL. To make sure that Umbraco actually finds the correct content, you could use a content finder. Or just the URL rewriting like we've discussed in this thread.
Thanks!
I'll try making a URL provider to go along with the URL rewrite rule, and if that doesn't work, I'll try switching out the URL rewrite for a content finder.
is working on a reply...