Help with Rewrite rules

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 5
Joined: 02 Apr 2012, 12:03

Help with Rewrite rules

02 Apr 2012, 12:13

These are my current rules.

Code: Select all
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d


RewriteRule ^([^/]+)$ /item.asp?ID=$1 [NC,L]

RewriteCond %{QUERY_STRING} ^ID=([^&]+)$ [NC]
RewriteRule ^item\.asp$ /%1? [NC,R=301,L]


What i want to do is add rules that appear before these rules that will rewrite http://www.website.com/aboutus.asp to http://www.website.com/aboutus

I also need http://www.website.com/category.asp?ID=119&page=1 to translate to http://www.website.com/Categoryname/page1

I need to add a few dozen similar rules and want the current rules above to run ONLY if none of the previous rules are matched.

I have tried several rules but they seem to either try to access a non-existent folder or try to run the rule that's already there after the correct rule, even if the last flag is set.

Any help would be greatly appreciated!

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: Help with Rewrite rules

03 Apr 2012, 05:34

Hello,

a) rules are processed in the order they appear in config file
b) your check for realness might not work because of the permissions to the folder. Those require NTFS permissions to ListFolderContent.

You may want to turn on logging and see how the rules are processed.
Logging issues described in FAQ

Regards
Andrew

User avatar
Posts: 5
Joined: 02 Apr 2012, 12:03

Re: Help with Rewrite rules

03 Apr 2012, 12:58

I changed the rewrite to work without the folder issue according to the logs, but on the redirect it is also getting caught in the overall rule

RewriteRule ^([^/]+)$ /item.asp?ID=$1 [NC,L]

Is there a way to exclude that rule on the redirect? I only want that rule active if none of the other rules above were valid at any point.

/Contact.asp is rewritten to /contact, then /contact matches the rule above and redirects to item.asp?id=contact which is NOT the desired behavior.

The rule above is for website.com/X1234 where X1234 is the item number... All of the item numbers follow the format X and 4 digits.

Any suggestions, when i try to change the overall rule to look for X then wildcard it goes into infinite redirect loop.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: Help with Rewrite rules

04 Apr 2012, 07:29

Here's a regular expression for a string starting with a letter and followed by 4 digits.
Code: Select all
RewriteRule ^([A-Za-z]\d{4})$ /item.asp?ID=$1 [NC,L]


Regards
Andrew

User avatar
Posts: 5
Joined: 02 Apr 2012, 12:03

Re: Help with Rewrite rules

04 Apr 2012, 14:12

I seem to be having an inordinate amount of trouble getting even the simplest thing to work. Does your company offer a service where they can write up the rules for us based on a list of requirements that i can email you.

Using the code you gave me above to modify my original rules, it goes into an infinite redirect loop as well.

Code: Select all

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([A-Za-z]\d{4})$ /item.asp?ID=$1 [NC,L]

RewriteCond %{QUERY_STRING} ^ID=([A-Za-z]\d{4})$ [NC]

RewriteRule ^item\.asp$ /%1? [NC,R=301,L]

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: Help with Rewrite rules

05 Apr 2012, 06:25

Hi,

a) Yes, we have Premium Support Plan where we do everything for you, using online meeting, skype or RDP. Simply go to Support tab on top of the site.

b) I suspect you have other rules taking part in it. to make sure it's not these 2 I'd use NS flag to make sure no other subrequest is running after the first rule:

Code: Select all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z]\d{4})$ /item.asp?ID=$1 [NC,L,NS]

RewriteCond %{QUERY_STRING} ^ID=([A-Za-z]\d{4})$ [NC]
RewriteRule ^item\.asp$ /%1? [NC,R=301,NS,L]


Regards
Andrew

User avatar
Posts: 5
Joined: 02 Apr 2012, 12:03

Re: Help with Rewrite rules

05 Apr 2012, 09:56

Log info

Code: Select all
 (2) init rewrite engine with requested uri /X1502
 (1) Htaccess process request C:\Program Files\Helicon\ISAPI_Rewrite3\httpd.conf
 (1) Htaccess process request c:\inetpub\website\.htaccess
 (3) applying pattern '^([A-Za-z]\d{4})$' to uri 'X1502'
 (4) RewriteCond: input='C:\inetpub\website\X1502' pattern='!-f' => matched
 (4) RewriteCond: input='C:\inetpub\website\X1502' pattern='!-d' => matched
 (1) Rewrite URL to >> /item.asp?ID=X1502
 (2) rewrite 'X1502' -> '/item.asp?ID=X1502'
 (2) internal redirect with /item.asp?ID=X1502 [INTERNAL REDIRECT]

 (2) init rewrite engine with requested uri /item.asp?ID=X1502
 (1) Htaccess process request C:\Program Files\Helicon\ISAPI_Rewrite3\httpd.conf
 (1) Htaccess process request c:\inetpub\website\.htaccess
 (3) applying pattern '^([A-Za-z]\d{4})$' to uri 'item.asp'
 (3) applying pattern '^item\.asp$' to uri 'item.asp'
 (4) RewriteCond: input='ID=X1502' pattern='^ID=([A-Za-z]\d{4})$' => matched
 (1) escaping /X1502
 (2) explicitly forcing redirect with http://www.website.com/X1502
 (2) internal redirect with /item.asp?ID=X1502 [INTERNAL REDIRECT]


Complete Rules File
Code: Select all
# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.89

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z]\d{4})$ /item.asp?ID=$1 [NC,L,NS]

RewriteCond %{QUERY_STRING} ^ID=([A-Za-z]\d{4})$ [NC]
RewriteRule ^item\.asp$ /%1? [NC,R=301,NS,L]


has a dozen or so entries in the log per page load, just keeps repeating itself until page times out.

I will take a look at the Support tab.

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: Help with Rewrite rules

05 Apr 2012, 10:12

The most common reason - double registration.
Please, follow the steps from the article

Regards
Andrew

User avatar
Posts: 5
Joined: 02 Apr 2012, 12:03

Re: Help with Rewrite rules

05 Apr 2012, 11:22

That took care or the redirect loop! Will now try adding back the other rules, thanks!

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 10 guests