Reputation: 21
It's my first time to use web.config for url rewrite to hide .php and .htm extension for files running on IIS sever. By google search, I got this code to hide the extension for profile.htm and contact.php.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RewriteRules">
<match url="profile.htm$" />
<action type="Rewrite" url="profile" />
<match url="contact.php$" />
<action type="Rewrite" url="contact" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
However, after I added this web.config file at the root directory on server, it gave me "500 internal server error" message and none of pages worked. Is there any syntax error or something else? Can I get help to fix this?
Another question: I also wanted to link different page in the .htm or .php code to hide .htm or .php extension too. For example, I previously used contact for href link, now I'd like to point it a directory without extension contact. Can the url rewrite do the same job to this or do I need to do something else?
Thanks!
Upvotes: 2
Views: 5133
Reputation: 1723
Below is how you can do it (it does not utilize rewrite maps -- rules only, which is fine for small amount of rewrites/redirects):
This rule will do SINGLE EXACT rewrite (internal redirect) /yourpage to /yourpage.html. URL in browser will remain unchanged.
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRewrite" stopProcessing="true">
<match url="^yourpage$" />
<action type="Rewrite" url="/yourpage.html" />
</rule>
</rules>
</rewrite>
301 redirect (Permanent Redirect) where URL will change in browser
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRedirect" stopProcessing="true">
<match url="^yourpage$" />
<action type="Redirect" url="/yourpage.html" />
</rule>
</rules>
</rewrite>
Attempt to execute such rewrite for ANY URL if there are such file with .html extension (i.e. for /yourpage it will check if /yourpage.html exists, and if it does then rewrite occurs):
<system.webServer>
<rewrite>
<rules>
<rule name="DynamicRewrite" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}\.html" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="/{R:1}.html" />
</rule>
</rules>
</rewrite>
Upvotes: 4