in

ASP.NET Weblogs

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

Leave a Comment

(required)  
(optional)
(required)  
Add