Isapi Rewrite 301 redirect resolves as 404 - circular reference?
-
20-08-2019 - |
Question
I'm trying to use IIS Isapi Rewrite to do the following...
I need seo-friendly URLs to be (silently) converted back to application friendly URLs like so:
RewriteRule ^/seo-friendly-url/ /test/index.cfm [I,L]
Simple enough.
But I also need URLs already indexed in search engines (for example) to be 301 redirected to the seo-friendly version. Like so:
RewriteRule ^/test/index.cfm /seo-friendly-url/ [I,R=301]
Each of these works fine in isolation. But when I have both in my .ini file I end up with /seo-friendly-url/ showing in my browser address bar but I'm being served a 404. (Yes, /test/index.cfm definitely exists!)
I know it looks like a circular reference, but the first rule only rewrites the URL between IIS and the application - there's no redirect, so I'm not hitting Isapi Rewrite a second time. Or am I wrong about that?
I've enabled logging on Isapi Rewrite and I'm seeing the following:
HttpFilterProc SF_NOTIFY_PREPROC_HEADERS
DoRewrites
New Url: '/seo-friendly-url/'
ApplyRules (depth=0)
Rule 1 : 1
Result (length 15): /test/index.cfm
ApplyRules (depth=1)
Rule 1 : -1
Rule 2 : 1
Result (length 18): /seo-friendly-url/
ApplyRules: returning 301
ApplyRules: returning 1
Rewrite Url to: '/seo-friendly-url/'
Anyone got any ideas?
Solution 2
Through some trial and error I've come up with a solution for this.
Specify that the redirect match is at the end of the string using the $ symbol:
RewriteRule ^/test/index.cfm$ /seo-friendly-url/ [I,R=301]
Make the rewritten URL trivially different from the redirect match string - in this case adding an unnecessary "?":
RewriteRule ^/seo-friendly-url/ /test/index.cfm? [I,L]
OTHER TIPS
You have two different rewrites here and it should work if you do it right
The first one is never seen by the client user-agent. It requests /seo-friendly and you rewrite it internally and respond
The second one is not really a rewrite, but a redirect. You send that back to the client and it re-requests the /seo-friendly -- I think you need to use [R=301,L] to say that this is the end of the line -- just return it (L does that)