问题描述:

We have a hosted Umbraco site that is using the UrlRewriting.net config file to handle redirects. The client has about 60 landing domains that they have bought and pointed at the site, however they want to 301 redirect all the landing domains to the actual domain along with anything after the domain, for this example I'll call it http://www.mainsite.com/

I'm using the below regex, which redirects anything that isn't the main site back to the mainsite with the pages appended.

<add name="GlobalRedirect"

virtualUrl="https?:\/\/((?!www\.mainsite\.co\.uk).*)\/(.*)"

rewriteUrlParameter="ExcludeFromClientQueryString"

destinationUrl="http://www.mainsite.co.uk/$2"

redirect="Domain"

redirectMode="Permanent" />

http://www.anything.co.uk/a-page is correctly getting redirected to http://www.mainsite.co.uk/a-page.

However http://www.anything.com/subpage/another-page is getting redirected to http://www.mainsite.com/another-page and missing the /subpage/.

The error is outside of the negative lookahead, but I can't work out how to fix it.

Any help is much appreciated.

网友答案:

Your regex is matching too greedily. If you change the RegEx to this:

https?:\/\/((?!www\.mainsite\.co\.uk).*?)\/(.*)

It will match only until it hits the first forward slash after the domain. So $2 should contain subpage/another-page in your example.

I can highly recommend a tool called Expresso for writing and testing Regular Expressions. It allows you to break down what a RegEx is doing, and run tests against them.

相关阅读:
Top