theGeekster
theGeekster

Reputation: 6241

Php: Rewrite URL using web.config

This my first PHP application, I am using both .html and .php pages in this website. If user browses mysite.com/users/?abc123, it successfully loads details of user with id 'abc123' on plain html page mysite.com/users/index.html via Ajax. Now, I am tasked to remove ? from the URL so that if user browses mysite.com/users/abc123, then mysite.com/users/index.html?abc123 should serve the details successfully.

I followed this link and added this rule to my web.config but that didn't seem to work and I received:

Error: HTTP Error 404.0 - Not Found

<rule name="Remove question mark" patternSyntax="Wildcard" stopProcessing="true">
  <match url="*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^users/(.*)$" />
  </conditions>
  <action type="Redirect" url="users/?{R:0}" redirectType="Permanent" />
</rule>

Please assist me with following concerns in mind:

  1. I can only use web.config for URL rewriting (to keep things simple)
  2. The page I want to rewrite URL for is .HTML not .PHP (if that matters)
  3. I am testing my PHP site locally in IIS 8 configured to serve PHP

Upvotes: 4

Views: 2218

Answers (1)

Justin Iurman
Justin Iurman

Reputation: 19026

This should be working

<rule name="Remove question mark" stopProcessing="true">
  <match url="^/?users/([^/]+)$" ignoreCase="true" />
  <conditions logicalGrouping="MatchAll">
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Rewrite" url="/users/index.html?{R:1}" />
</rule>

Upvotes: 1

Related Questions