Help with writing rules

Topics: Developer Forum, User Forum
Jun 29, 2008 at 6:43 PM
Edited Jun 29, 2008 at 8:59 PM
Hi

I am new with url rewrite and would like to take some help from the experts on the same.

My requirements are as follows:

1- I need to have urls in the form: http://websitename.com/anytext, and it should be forwarded to http://websitename.com/somefile.aspx?txt=anytext
2- I also have some files with aspx, axd and other extensions which I would not want to be redirected.
3- There is a possibility for urls like http://websitename.com/anytext/somenumber and it should be fwd as above http://websitename.com/somefile.aspx?txt=anytext/somenumber 
4- The site need not run using www., so if someone types www.websitename.com, I need to change it to websitename.com.

I have read through the readme file and seen the examples as well and successfully installed the files to right places. I am currently using:
RewriteRule ^/([^/]+)/([^/]+)$  /default.aspx?txt=$1/$2    [U,I,L]
as my rule, which I am sure would not be correct, for all the cases as stated above.

Thanks
Sandy
Coordinator
Jun 30, 2008 at 11:13 PM
2 questions:

what, specifically, are you seeing? (in the browser, and in the log files)
what, specifically, are you expecting to see?  (in the browser, and in the log files)

Also - I think it is best to break down the situation into parts.  You have a number of requirements.  Can we focus on one requirement at a time, please? 
It seems to me that #3 is just a generalized form of #1, so we can consider both of them together.

Jul 1, 2008 at 1:08 PM
Hello Cheeso,

Thanks for your reply.

I have moved 1 step ahead with this and have written a new .ini file.

#pass all image urls unchanged
RewriteRule ^/images/(.*)$ /images/$1 [I,L]

#pass all image urls unchanged
RewriteRule ^/lib/(.*)$ /lib/$1 [I,L]

RewriteRule ^(.*\.axd)$ $1 [I,L]

# now pass through to the generic handler
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ /default.aspx?city=$1 [I,L]

Now hopefully my only requirement should be 4- The site need not run using www., so if someone types www.websitename.com, I need to change it to websitename.com. You can review the above statements and let me know if anywhere it is incorrect. Hopefully this should work but as I am using a shared hosting, my host has copied the .ini at the following path httpdocs/isapi/isapirewrite4.ini, now this seems to be in conflict with the readme file, which says that the ini file should not share the path with the website, all other website files are directly under httpdocs/. And for me I dont see the changes made to the ini file being reflected on server anymore.

Is there any way I can overcome this, coz in a shared hosting the ini file would always be stored somewhere on a similar path as this.

Thanks
Sandy

Coordinator
Jul 1, 2008 at 5:36 PM

Hi Sandy,

to change from www.site.com to site.com , you need a redirect.   This means the address changes in the browser address bar.  That's what you want, right?
There are lots of examples of this.  The key thing is to use rewriteCond on the HTTP_HOST and then use the [R] flag on the rule.
Like so:

RewriteCond %{HTTP_HOST}  ^(?!www\.)site1\.com$   [I]
RewriteRule ^/(.*) http://www.site1.com/$1 [R=301]

This rule says, if the HTTP_HOST is site1.com, then redirect it (R=301) to www.site.com, using the same URL request. This will send back a message to the requesting browser. The browser will get the HTTP 301 return code, will update the address in the address bar, and then submit a new request. So if the browser originally asks for http://site1.com/Page.aspx, then it will get the 301 and send out a new request to http://www.site1.com/Page.aspx. Does that make sense?

Finally, you said that the readme file "says that the ini file should not share the path with the website." That is not correct, I believe. The readme file does not say that. Maybe read it again and show me where you are getting this information? The key thing is this: the ini file should be in a directory which is different from the directory that contains the logfile. Check the readme again. You also said I dont see the changes made to the ini file being reflected on server anymore. The rules are very simple - the ini file must be in the same directory as the DLL. IS that the case in your shared hosting environment? I cannot tell from what you have written.

Jul 1, 2008 at 6:42 PM
Hello Cheeso

You are correct about the part 1 of your answer, just that you did it the other way round. i.e. u did for site.com to www.site.com, whereas I needed www.site.com to site.com.

Secondly, The instruction 1 from the read me file states:
"1. Copy the filter DLL to an appropriate folder, such as c:\windows\system32\inetsrv .   Or it could be in c:\wwwroot somewhere.  Or it could be in a totally separate directory tree, with permissions granted to the appropriate Windows principal (IUSR_hostname).  It's up to you.   The only thing - don't put it in the web document tree.  (it causes problems with detecting changes in the ini file.  there's more about this later in the readme). "
It is possible that I have interpretted it incorrectly.

My dll and ini files are in same folder. So that is not a problem. But may be the ini I have created is having some problems and that is creating issues with the site. Kindly comment on the ini statements pasted below:

#pass all image urls unchanged
RewriteRule ^/images/(.*)$ /images/$1 [I,L]

#pass all image urls unchanged
RewriteRule ^/lib/(.*)$ /lib/$1 [I,L]

RewriteRule ^(.*\.axd\?.*)$ $1.axd?$2 [I,L]            # I am doubful abt this one. This should pass all .axd files with the querystrings as it is.

# now pass through to the generic handler
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ /default.aspx?city=$1 [I,L]


Thanks
Sandy
Coordinator
Jul 2, 2008 at 5:50 AM
RewriteRule ^(.*\.axd\?.*)$ $1.axd?$2 [I,L]            # I am doubful abt this one. This should pass all .axd files with the querystrings as it is.

The above is not right. What you have is a regex with a single capture. The capture is everything between the parens. This means you can reference $1 in the replacement string, but not $2. There is no 2nd capture, therefore you cannot reference $2. Ya follow? The $1 capture is "everything" in the URL request, including the leading slash, and the question mark.  Keep in mind since you specifically included a question mark, this regex matches ONLY URL requests that have a query string.  It will match http://foo.com/fribble.axd?ralph  but it will not match http://foo.com/fribble.axd

If that is what you want to match on - .axd requests with a query string, then I think what you want is something like this:

RewriteRule ^(.*\.axd\?.*)$  $1   [I,L]

On the readme, thanks for the pointer. I think the information in the readme is not correct.  I'm very sorry about that.  The actual restriction, as I recall, is "don't put it the ini file in the same directory as the metabase; and don't put the logfile in the same directory, or in a subdirectory, of the directory that contains the ini file."

I will let you figure out how best to work it for your hoster.

Sorry about the confusion.

Jul 2, 2008 at 6:36 AM

Thanks for all your help.

But my host has now refused to continue me with the use of ISAPIReWrite anymore. So I have to either use url mapping to redirect urls or need to look for some other host. Do you have in knowledge any host that provides support for this component, and is also cheap, my current hosting costs around $6 pm.

Thanks again

Sandy

Coordinator
Jul 2, 2008 at 6:57 AM
sorry about the trouble.
good luck.