user135383
user135383

Reputation: 661

How do I use a pattern Url to extract a segment from an actual Url?

If I have a series of "pattern" Urls of the form:

http://{username}.sitename.com/
http://{username}.othersite.net/
http://mysite.com/{username}

and I have an actual Url of the form:

http://joesmith.sitename.com/

Is there any way that I can match a pattern Url and in turn use it to extract the username portion out the actual Url? I've thought of nasty ways to do it, but it just seems like there should be a more intuitive way to accomplish this.

ASP.NET MVC uses a similar approach to extract the various segments of the URL when it is building its routes. Given the example:

{controller}/{action}

So given the Url of the form, Home/Index, it knows that it is the Home controller calling the Index action method.

Upvotes: 0

Views: 269

Answers (5)

user135383
user135383

Reputation: 661

It seems the the quickest and easiest solution is going off of Machine's answer.

var givenUri = "http://joesmith.sitename.com/";
var patternUri = "http://{username}.sitename.com/";
patternUri = patternUri.Replace("{username}", @"([a-z0-9\.-]*[a-z0-9]");

var result = Regex.Match(givenUri, patternUri, RegexOptions.IgnoreCase).Groups;

if(!String.IsNullOrEmpty(result[1].Value))
    return result[1].Value;

Seems to work great.

Upvotes: 1

jscharf
jscharf

Reputation: 5899

There are definitely different ways - ultimately though your server must be configured to handle (and possibly route) these different subdomain requests.

What I would do would be to answer all subdomain requests (except maybe some reserved words, like 'www', 'mail', etc.) on sitename.com with a single handler or page (I'm assuming ASP.NET here based on your C# tag).

I'd use the request path, which is easy enough to get, with some simple string parsing/regex routines (remove the 'http://', grab the first token up until '.' or '/' or '\', etc.) and then use that in a session, making sure to observe URL changes.

Alternately, you could map certain virtual paths to request urls ('joesmith.sitename.com' => 'sitename.com/index.aspx?username=joesmith') via IIS but that's kind of nasty too.

Hope this helps!

Upvotes: 0

PatrikAkerstrand
PatrikAkerstrand

Reputation: 45721

A very simple regex will do:

':https?://([a-z0-9\.-]*[a-z0-9])\.sitename\.com'

This will allow any subdomain that only contains valid subdomain characters. Example of allowed subdomains:

  • joesmith.sitename.com
  • joe.smith.sitename.com
  • joe-smith.sitename.com
  • a-very-long-subdomain.sitename.com

As you can see, you might want to complicate the regex slightly. For instance, you could limit it to only allow a certain amount of characters in the subdomain.

Upvotes: 1

DmitryK
DmitryK

Reputation: 5582

Not sure I understand this question correctly but you can just use a regular expression to match anything between 'http://' and the first dot.

Upvotes: 1

Noon Silk
Noon Silk

Reputation: 55072

Well, this "pattern URL" is a format you've made up, right? You basically you'll just need to process it.

If the format of it is:

  • anything inside "{ }" is a thing to capture, everything else must be as is

Then you'd just find the start/end index of those brackets, and match everything else. Then when you get to a place where one is, make sure you only look for chars such that they don't match whatever 'token' comes after the next ending '}'.

Upvotes: 0

Related Questions