Create Rewrite rules for URL rewrite module in web.config
Today i will explain you simple and concise way to create rewrite rules for URL rewrite module in web.config, which might panic for most of the developers/users.
- First of all install url rewrite 2.0 module for IIS 7 or above from here. (download 32 or 64 bit according to system)
- And add below code in web.config file and save.
<system.webServer> ... <rewrite> <rules> <rule name="Rewrite to article.aspx"> <match url="^news/([0-9]+)" /> <action type="Rewrite" url="article.aspx?id={R:1}" /> </rule> </rules> <outboundRules> <rule name="Rewrite to clean URL" preCondition="IsHTML"> <match filterByTags="A" pattern="^article\.aspx\?id=([0-9]+)$" /> <action type="Rewrite" value="news/{R:1}" /> </rule> <preConditions> <preCondition name="IsHTML"> <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" /> </preCondition> </preConditions> </outboundRules> </rewrite> ... </system.webServer>
- Above code will add to url rewrite module in IIS.
- Now in address bar you can test any of your .net application as example below:
http://localhost/article.aspx?id=342 - It will turn to http://localhost/news/342
Please note here page name is used as different- news in above example, here user can place with any page name which want to show in SEO. - Most important: in .aspx or .aspx.cs file the url should be placed as shown below sample:
<a href=”article.aspx?id=[table_id]”>click here</a>.
Here [table_id] will be replace as your dynamic id using a variables. - Similarly when there are more pages you prefer to do SEO friendly then each rule have to call in web.config to match in rule and outbound rules.
Hope this work well who are keen to do and reply your comment.