The ASPSmith's Blog

Some rants about ASP.NET by Steven Smith

Regex To The Rescue For Shorter URLs

I've been redesigning AspAlliance.com off and on for the last several months, and I made a few more changes this morning.  The big one that is noticeable to the general public is the URLs.  Instead of having to link to articles via a viewer ASPX page and a series of querystring values, it is now sufficient to simply append the article ID to the end of the domain name (after a slash), like so:

http://aspalliance.com/1  (article ID 1, which is my Excel Reports in ASP article).

The nice thing about this is that it uses Context.RewritePath, so there is no Response.Redirect and the user never sees the actual URL of the page handling the request.  The regex I'm using is here:

http://regexlib.com/REDetails.aspx?regexp_id=456

The actual code looks like this:

string originalUrl = Request.Url.ToString();
// Check for article shortcuts (e.g. http://aspalliance.com/1 )
string newUrl = AspAlliance.Web.Core.HttpRedirect.GetRedirect(originalUrl);
if(newUrl != originalUrl)
{
System.Uri myUri =
new System.Uri(newUrl);
Context.RewritePath(myUri.PathAndQuery);
}

// GetRedirect:

System.Text.RegularExpressions.Regex regex =
new System.Text.RegularExpressions.Regex(@"\.com/(\d+)$",
(System.Text.RegularExpressions.RegexOptions.Compiled |
System.Text.RegularExpressions.RegexOptions.IgnoreCase));
System.Text.RegularExpressions.MatchCollection matches = regex.Matches(badRequest);
if(matches.Count > 0)
{
  string id = matches[0].Value.Replace(".com/", "");
  int aId;
  try
  {
    aId = Int32.Parse(id);
    return "http://aspalliance.com/articleviewer.aspx?aId=" + id;
  }
  catch
  {}
}
return badRequest;

 

Comments

Chad Grant said:

but... id you do this: it doesnt work

http://aspalliance.com/1/

Sucks doesnt it? Supposedly this is fixed in asp.net 2.0
# November 6, 2003 2:01 PM

Steven Smith said:

If I wanted that to work, I could capture it easily enough. For the moment, though, I don't want trailing slashes to go to articles, since I may want to use those for actual folders at some point.

But certainly, I can make it work today simply by adjusting my regex to include something like

/?

at the end of it (for 0 or 1 slash characters).

NOTE: I'm handling ALL requests through the ASP.NET process, which is why this will work for me.
# November 6, 2003 2:26 PM

Chad Grant said:

The point is, if a user knows your article ID , it's VERY likely for them to type the extra slash.....

I use rewriting in sites I want to for example http://domain/chad

There is a ISAPI Rewriter that I use : http://www.isapirewrite.com/

it's free
# November 6, 2003 4:48 PM

TrackBack said:

# December 25, 2003 5:40 PM

<!-- said:

<!--
# March 17, 2004 6:18 AM

TrackBack said:

# July 6, 2004 1:11 AM

TrackBack said:

# July 6, 2004 7:14 PM

seo yarışması said:

If I wanted that to work, I could capture it easily enough. For the moment, though, I don't want trailing slashes to go to articles, since I may want to use those for actual folders at some point

# September 3, 2007 4:50 PM

Kalkan Antalya said:

can i put this article on my weblogs_?

# September 25, 2007 4:59 PM

www.r10.net küresel ısınmaya hayır seo yarışması said:

since I may want to use those for actual folders at some point

# October 22, 2007 4:33 AM

asd said:

# November 4, 2007 2:36 PM

Plastik Ambalaj said:

Burasıda çöplüğe dönmüş :D

<a href="www.gurplastikambalaj.com " title="Plastik Sanayi ">Plastik Sanayi</a>

# November 5, 2007 9:24 AM

Çadır said:

Thanks.good Source

<a href="http://www.zemakina.com/" title="çadır,branda,çadır branda,çadır imalatı,şantiye çadırı">Çadır,Branda,Şantiye Çadırı,Çadır Branda,Çadır imalatı</a>

# November 5, 2007 9:36 AM

Regex To The Rescue For Shorter URLs said:

Pingback from  Regex To The Rescue For Shorter URLs

# November 27, 2007 9:03 PM

Load cell said:

Thanks.good Source

# December 10, 2007 3:18 AM

youtube said:

<a href="www.rizecity.com/">Youtube, film indir, rize</a>

# December 15, 2007 3:52 PM

hikaye said:

"I was paid to trash this product." That was priceless Dan.

# December 20, 2007 8:28 AM

wmwebtr ödüllü seo yarışması said:

SEO (Search Engine Optimization) yarışmasınının genel amacı "wmwebtr ödüllü seo yarışması" kelimesinde google da ilk üçe girebilmektir. İlk üçe girenlere sürpriz hediyeler var.

# December 20, 2007 3:25 PM

www.r10.net Küresel Isınmaya Hayır Seo Yarışması said:

Amaç: hem eğlenmek hemde yarışma coşkusunu yaşatacak forum etkinliğidir. Bu yarışma sonucunda ilk sırada olan arkadaşımız 2007 yılının en iyi SEO'cusu seçilecek ve ödüllendirilecekdir.Ayrıca dereceye giren ilk üç kişiye sürpriz hediyeler var.

# December 24, 2007 4:23 PM

mirc said:

Dunyanın bir numaramı mirc programını indirmek sizin icin artik bir tik yakınında turkiyemirc farkıyla bu mircmizi indirin sizde sohbet dunyasının en güzel alemine adımınızı atın hoş muhabbetler herkese

# January 5, 2008 5:19 PM

Football Manager said:

very useful document,thanks

# January 6, 2008 2:16 PM

小马 said:

# January 11, 2008 4:01 AM

güzel sözler said:

can i put this article on my weblogs_?

# January 12, 2008 10:40 PM

igorus said:

очень интересеная статья !!!

Thanks/

# January 19, 2008 12:17 AM

Lazer Kesim said:

thanks for this post

# January 26, 2008 10:47 AM

hikaye said:

This company is tanking, I hope to be able to short this, but I am betting it will remove itself from an ipo with all the bad publicity....

# February 26, 2008 5:35 AM

mirc said:

Successful website

# March 14, 2008 3:51 PM

Murat Kekilli said:

# April 5, 2008 9:04 AM

netlog said:

thank you very good

# April 7, 2008 9:46 AM

oyun said:

very useful document,thanks

# April 20, 2008 6:20 PM

laptop masası said:

This company is tanking, I hope to be able to short this, but I am betting it will remove itself from an ipo with all the bad publicity.... Thank You

# May 5, 2008 1:31 PM

Web Sayfa Tasarımı said:

Freelance Web Tasarım , Web Sayfa Tasarımı , Arama Motoru Optimizasyonu , SEO , AMO , Flash Web Site Tasarımı ,Web Dizayn , Web Design , İnternet Reklamcılığı

# May 21, 2008 10:42 AM

rize otelleri said:

If I wanted that to work, I could capture it easily enough. For the moment, though, I don't want trailing slashes to go to articles, since I may want to use those for actual folders at some point

# June 8, 2008 12:27 PM

rize otelleri said:

This company is tanking, I hope to be able to short this, but I am betting it will remove itself from an ipo with all the bad publicity.... Thank You

<a href="http://www.besikcihotel.com" title="rize otelleri" target="_blank">rize otelleri</a>

# June 9, 2008 1:51 AM

eglenclub said:

Thank you so much. I appreciate your work. Many thanks.

# June 16, 2008 6:11 AM

intBuilding » ??????ASP.NET 2.0???URL?????? said:

Pingback from  intBuilding &raquo; ??????ASP.NET 2.0???URL??????

# July 11, 2008 8:22 PM

Success stories with lexapro. said:

Lexapro. Lexapro focalin xr.

# July 27, 2008 1:34 AM

hikaye said:

If I wanted that to work, I could capture it easily enough. For the moment, though, I don't want trailing slashes to go to articles, since I may want to use those for actual folders at some point

# July 29, 2008 3:18 PM

büyütücü haplar said:

Thank you so much. I appreciate your work. Many thanks.

# August 10, 2008 6:06 AM

buyutucu said:

appreciate your work. Many thanks.

# August 11, 2008 2:09 PM

cep telefonu said:

thank you very good

# September 7, 2008 3:01 AM

teyze said:

If I wanted that to work, I could capture it easily enough. For the moment, though, I don't want trailing slashes to go to articles, since I may want to use those for actual folders at some point www.izleglen.com/.../Azgin_Karisini_Siktim

# September 23, 2008 5:31 AM

Sohbet said:

<a href="http://www.idealsohbet.Com" title="sohbet, chat" target="_blank">Sohbet</a>

thanks you

# October 15, 2008 9:10 AM

kazalar said:

I am betting it will remove itself from an ipo with all the bad publicity

# October 26, 2008 3:50 PM

hediyelik eşya said:

thanks <a title="hedyelik eşya" href="http://www.oltuincisi.com">hediyelik eşya</a>

# November 2, 2008 3:47 AM

Estetik said:

I wasn’t blaming you, it was my (sometimes lack of) English - not my native language. Glad I got want you meant in the end (and also everything from your latest link).

Asked typepad about how to remove no-follow, received a nice message back from them, but have to find time now to check their suggestions out (if I can work it out, that is).

# November 13, 2008 10:36 AM

saç ekimi said:

I wasn’t blaming you, it was my (sometimes lack of) English - not my native language. Glad I got want you meant in the end (and also everything from your latest link).

# November 24, 2008 5:34 PM

muhabbet said:

Thanks web blog very good

# December 10, 2008 10:52 AM

oyun said:

ı have followed your writing for a long time.really you have given very successful information.

In spite of my english trouale,I am trying to read and understand your writing.

And ı am following frequently.I hope that you will be with us together with much more scharings.

I hope that your success will go on.

# December 18, 2008 9:33 PM

chat said:

<a href="http://www.trtsar.net" title="chat, sohbet chat">chat</a>  tşkk  <a href="http://www.trtsar.net" title="chat, sohbet chat">sohbet</a>

# December 19, 2008 12:17 PM

Bahreyn Ucak Bileti said:

thanks for the post

# December 21, 2008 5:38 PM

seslichat said:

# December 25, 2008 9:22 AM

sohbet odaları said:

# January 2, 2009 6:52 AM

sohbet odaları said:

<a href="http://www.sohbet39.com" title="sohbet, sohbet odaları">sohbet odaları</a>  

# January 2, 2009 6:56 AM

Çocuk Oyunları said:

Will try it out but worriying about the future of .net - in hands of MS ..

# January 14, 2009 8:06 AM

cam balkon said:

cam balkon sistemleri thanks admin

# January 20, 2009 2:43 PM

sohbet said:

<a href="http://www.sohbetci.com.tr" target="_blank" title="Sohbet, chat, sohbet odaları">Sohbet</a>

# January 24, 2009 3:07 AM

halı yıkama makinaları said:

thank site very much

# March 24, 2009 5:40 AM

webmoney said:

So match interessing, 10x!

# April 19, 2009 3:38 PM

online-dinle said:

thanx for article admin very nice thanx

# April 20, 2009 9:47 AM

sohbet odaları said:

THNKS

Sohbet and Muhabbet

# May 4, 2009 12:12 PM

Güzel Sözler said:

# May 4, 2009 2:56 PM

halimdiktas said:

thanks

www.halimdiktas.tr.gg

# May 6, 2009 5:27 AM

mirc said:

www.takintidivx.net

# May 7, 2009 11:53 AM

çet said:

thanks for comment

# May 24, 2009 6:12 PM

sohbet said:

thanks admin good post

# June 8, 2009 10:20 AM

sohbet said:

thanks admin good post

# June 16, 2009 11:30 AM

TV studio film lighting said:

Thanks a lot. keep it up.

# June 17, 2009 2:19 AM

cam balkon said:

thankssyouu admin.

# June 24, 2009 4:37 PM

cam balkon said:

thankss youu katlanır camlama.

# June 24, 2009 4:45 PM

kameralı sohbet said:

thanks you very much so

# June 26, 2009 9:48 AM

HD video camera battery said:

Thanks a lot. keep it up.

# July 3, 2009 1:22 AM

medyum said:

I wasn’t blaming you, it was my (sometimes lack of) English - not my native language. Glad I got want you meant in the end (and also everything from your latest link). http://www.medyum.gen.tr

# July 4, 2009 9:40 AM

neon said:

Everything is very open and very clear explanation of issues. was truly information. Your website is very useful. Thanks for sharing.

# July 4, 2009 12:45 PM

sohbet said:

thanks admin good post

# July 5, 2009 9:21 AM

sohbet said:

thanks admin good post

# July 7, 2009 5:51 AM

hikaye said:

This looks very good and well made.

# July 17, 2009 9:50 AM

aöf said:

Very good, congratulations article

# July 17, 2009 4:56 PM

cinsellik said:

I am grateful to you for this great content.

# July 18, 2009 10:10 AM

güzel sözler-anlamlı sözler said:

Weeryy  good admin

# July 18, 2009 3:47 PM

radyo dinle said:

thanks admin good post

# July 19, 2009 11:09 AM

cam balkon said:

# July 22, 2009 1:30 PM

kameralı sohbet said:

# July 23, 2009 7:22 PM

sohbet said:

# July 30, 2009 6:32 AM

chat siteleri said:

Great job on the conversion but I have to say that I prefer the real thing. But…if I had to play guitar hero, I would prefer to play it on your conversion guitar rather than the original.

# August 1, 2009 1:39 PM

izmir web tasarım said:

Thanks for posting about this, I would love to read more about this topic.

# August 4, 2009 10:47 AM

chat sitesi said:

In this case, being essentially considered a criminal (and yes, they do consider legitimate webcasters criminals-- it's only a matter of time before they come out and say it...) even though you have tried to play by the rules is wrong, but otherwise unremarkabl

# August 5, 2009 5:22 AM

medyum said:

Pingback from  Regex To The Rescue For Shorter URLs

# August 10, 2009 8:36 AM

saç ekimi said:

saç ekimi and hair transplant method sites: www.bogazicisacekimi.com

# August 11, 2009 10:10 AM

chat said:

Thanks a load for the stats. Haven’t seen anything nearly as comprehensive or useful anywhere else.

# August 16, 2009 11:48 AM

cam balkon said:

<a href="http://www.ftkcambalkon.com" title="cam balkon">cam balkon</a>

# August 19, 2009 10:00 AM

sohbet said:

Very cool app. Can you tell me where to download the Help files? Apparently they didn’t make it during the install process, so it won’t load them. Thanks!

# August 27, 2009 3:29 PM

grow taller said:

For the moment, though, I don't want trailing slashes to go to articles, since I may want to use those for actual folders at some point.

# August 27, 2009 5:00 PM

alevi sohbet said:

Given that they still have a silly religious devotion to keeping the front page "clean", it's a wonder their other products have done as well as they have. But many of them are making progress, slowly but surely, and I suspect one day Sergey and Larry will realize that starting with "search" is actually less convenient than starting with clicks and build out the front page.

# August 30, 2009 8:33 PM

chat said:

Thanks a load for the stats. Haven’t seen anything nearly as comprehensive or useful anywhere else.

# September 9, 2009 4:35 AM

cam balkon said:

<a href="http://www.dunyacambalkon.com" title="cam balkon">cam balkon</a>

# September 10, 2009 10:52 AM

chat said:

thanks for information admin

# September 14, 2009 6:14 PM

sohbet said:

Thanks for posting about this, I would love to read more about this topic.

# September 14, 2009 9:46 PM

özel ders said:

# September 16, 2009 5:01 PM

sohbet said:

thanks very mucks. www.kameraseslisohbet.com

# September 17, 2009 7:04 AM

sohbet said:

# September 25, 2009 1:28 PM

katlanır cam balkon said:

# September 30, 2009 7:00 AM

bursa said:

Everything is very open and very clear explanation of issues. was truly information. Your website is very useful. Thanks for sharing.

# October 4, 2009 4:14 AM

cam balkon said:

how to set just 1 column on main

# October 14, 2009 1:32 PM

cambalkon said:

thanks for performance

# October 20, 2009 9:15 AM

hürriyet seri ilan said:

thanks for performance adimn

# October 22, 2009 10:20 AM

hürriyet ilan said:

thanks admin very good

# October 28, 2009 10:34 AM

Sevgi büyüsü said:

thanks admin very good

# November 6, 2009 9:27 AM

Direct Mobile Phone Shop said:

Very nice and helpful article to us

Ads:www.directmobilephoneshop.co.uk

# November 8, 2009 1:54 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)