Help with simple rule

Jun 1, 2007 at 5:52 PM
Edited Jun 1, 2007 at 6:02 PM
Hello guys & gals,
This is a simple plea for help from one (not uber smart) geek to other very smart geeks! I have a need for URL rewriting, but I can't figure out what I am supposed to write for a rule, none of the sample rules seem to fit what it is that I need to do. Here is what I need:

I am using presstopia's .Net blog, and it puts everything after a quick redirect into /blog/ folder. So what I need to do is strip that /blog/ folder off my URL. I have a few sample URL's taken from my site, here they are:

/blog/
/blog/default.aspx?act=usr
/blog/default.aspx?id=157&t=Vision-for-Blind-With-Computer-Chip

These all need to be made into this:
/
/default.aspx?act=usr
/default.aspx?id=157&t=Vision-for-Blind-With-Computer-Chip


I wrote a rule that I thought might work, but I put it in an .ini file, and put it in the test folder with the above three URLs in the sampleurls.txt then I ran the testdriver.exe.

Here is the rule that I wrote:
    RewriteRule  ^/blog/(.*).*  /(.*).*    
 


I don't think it is working correctly as the testdriver results show me this:
REWRITE '/blog/' ==> '/(.*).*'
 
REWRITE '/blog/default.aspx?act=usr' ==> '/(.*).*'
 
REWRITE '/blog/default.aspx?id=157&t=Vision-for-Blind-With-Computer-Chip' ==> '/(.*).*'


Can someone please tell me what is wrong with this rule? I would greatly appreciate it!


Thanks everyone,
Jamie
Jun 1, 2007 at 7:22 PM
Hi there...

You have to escape periods...so your rule would look like this:

RewriteRule ^/blog/(.*\..*)$ /$1

What you're saying is; "anything coming to /blog/whatever.xxx... should be processed by /whatever.xxx..."

You could get more technical with specifying how may characters after the file name to capture {3,4}, but that rule should suffice I think.

If you want to play with RegEx I'd highly recommend you get Expresso; a freeware RegEx builder/tester - did wonders for me. Otherwise check out ILoveJackDaniels.com for some RegEx cheat sheets (I know, funny URL, but he has some very helpful stuff).

Best of luck!
Jun 1, 2007 at 10:26 PM
Thanks so much for your help,
I put this rule in my ini file and implemented the dll for my domain. But it is not working the way I need it to. Here is what is happening now:

everypage is now going to mydomain.com/blog/

So what WAS before the URL Rewriting:
www.mydomain.com/blog/default.aspx?id=157&t=Vision-for-Blind-With-Computer-Chip


IS NOW (With the rule that was posted for me for IIRF)
www.mydomain.com/blog/


WHAT I NEED IT TO BE
www.mydomain.com/default.aspx?id=157&t=Vision-for-Blind-With-Computer-Chip


Thanks I hope you can help,

Jamie



mcwilliams132 wrote:
Hi there...

You have to escape periods...so your rule would look like this:

RewriteRule ^/blog/(.*\..*)$ /$1

What you're saying is; "anything coming to /blog/whatever.xxx... should be processed by /whatever.xxx..."

You could get more technical with specifying how may characters after the file name to capture {3,4}, but that rule should suffice I think.

If you want to play with RegEx I'd highly recommend you get Expresso; a freeware RegEx builder/tester - did wonders for me. Otherwise check out ILoveJackDaniels.com for some RegEx cheat sheets (I know, funny URL, but he has some very helpful stuff).

Best of luck!

Jun 2, 2007 at 2:54 AM
Sorry, didn't follow you the first time.

What I would do is loose the query string, that's one of the main ideas of URL rewriting; provide "friendly URLs".

So a URL of:

/blog/default.aspx?id=157&t=Vision-for-Blind-With-Computer-Chip

I would recommend doing:

/157/Vision-for-Blind-With-Computer-Chip/

You'd write this like:

RewriteRule ^/(\d+)/(\w+)/$ /blog/default.aspx?id=$1&t=$2

So a URL of www.mysite.com/123/something-here/ would be processed by /blog/default.aspx?id=123&t=something-here.

All you're doing is catching what's coming in, and processing it where it needs to be, thus making the URL nice and pretty.

But... if you want all that query string stuff and just lop off the /blog/ part of the url...just do what I suggested before - only slightly updated:

RewriteRule ^/(\w+\.\w{3,4}.*)$ /blog/$1

So it reads as: anything coming in as /somefile.abc?asdf=23 is processed by /blog/somefile.abc?asdf=23

If you're sure that default.aspx will always be the default page, then you can just put that in there like ^/default.aspx(.*)$ but you'll need to amend the processing to be /blog/default.aspx$1.

To get www.mysite.com to be processed by www.mysite.com/blog/ you'll need a conditional rule to handle that OR just set your web server to serve pages from /blog as the root of your web and you can forget about all this rewriting stuff.

But if you need to (I'm guessing here...so don't be pissed if it doesn't work):

RewriteCond %{HTTP_HOST} ^www.mysite.com$
RewriteRule ^(.*)$ /blog/$1

But, if it were up to me...I'd just have your server serve from the /blog dir anyways...simpler. Hope that helps a little.
Jun 3, 2007 at 11:08 AM
Edited Jun 3, 2007 at 11:11 AM
Hi Jamie,

The original rule provided by mcwilliams132 looks correct, for your purposes you could probably simplify it a bit.

RewriteRule ^/blog/(.*)$ /$1 [I]

Thanks,
Ady