.htaccessurl-rewritinghelicontech

URL-Rewriting won't allow reg-ex as first paramater in address


I am using Helicon Ape in IIS7 for my 'mod rewrite' on Windows Server 2008.

Very simple rule:

RewriteRule ^([^\/]+) /index.php?page=$1 [L,NC]
www.example.com/mypage

This doesn't work, and I have experienced this on several sites. However, I have reached a problem where I am now transferring an existing site (with the above rule, which previously worked in IIS6, without Helicon Ape) to a new server, so I need to get it to work.

If I add an explicit parameter to my .htaccess before the regular-expression, it works fine:

RewriteRule ^page/([^\/]+) /index.php?page=$1 [L,NC]
www.example.com/page/mypage

I don't understand why this works and the previous example doesn't?


Solution

  • The following redirects /mypage or /mypage/ to /index.php?page=mypage

    RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L,NC]
    

    You were escaping the / but it's not required. (Only \ does.)