1

I am trying to rewrite the urls to make them search engine friendly

www.mydomain.com/qa/213/who-am-i

rewrites as

www.mydomain.com/qa/?qa=213/who-am-i

The below block works, but the problem is that the js/css/images urls inside the page are also rewritten. So the page looks for files like www.mydomain.com/qa/213/who-am-i/jquery.js which actually doesn't exist. So the page loads, but none of the css, .js and images work.

 <rule name="CleanRouting" stopProcessing="true">
          <match url="^qa/(.*)/(.*)$" ignoreCase="true" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="qa/?qa={R:1}/{R:2}&amp;{QUERY_STRING}" appendQueryString="false" />
        </rule>

Please tell me how to fix this. I am using Asp.Net MVC (if it matters).

Gautam Jain
  • 6,789
  • 10
  • 48
  • 67

1 Answers1

2

After several hours & days I finally found the correct IIS rewrite rule to be used for bypassing images/css/js etc files so that the page appear correctly.

The following rewrite rules must be added to your ASP.Net MVC project's Web.Config.

<!--Rewrite all paths containing a period like www.conceptworld.com/qa/453/who-am-i/qa-images/search.png to  www.conceptworld.com/qa/qa-images/search.png -->

<rule name="RewriteFileUrls" stopProcessing="true">
          <match url="^qa\/([^\/]*)\/([^\/]*)\/(.*[\.].*)$" ignoreCase="true" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="qa/{R:2}/{R:3}" appendQueryString="false" />
        </rule>

I am using the above rule plus the below 2 rules for the Question2Answer Question Answer PHP based solution similar to StackOverflow. I have installed Question2Answer on a Windows system with IIS + Asp.Net MVC.

<!--Rewrites urls like www.conceptworld.com/qa/tags -->

<rule name="RewriteSingleLevelUrls" stopProcessing="true">
          <match url="^qa\/([^\/]*)$" ignoreCase="true" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="qa/?qa={R:1}&amp;{QUERY_STRING}" appendQueryString="false" />
        </rule>

<!--Rewrites urls like www.conceptworld.com/qa/56/who-am-i -->
        <rule name="RewriteTwoLevelUrls" stopProcessing="true">
          <match url="^qa\/([^\/]*\/[^\/]*)$" ignoreCase="true" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="qa/?qa={R:1}&amp;{QUERY_STRING}" appendQueryString="false" />
        </rule>

Just setting the above rules is not enough. What is most important is setting your base url in Question2Answer. I almost gave up and thought it was impossible to have pretty SEO friendly urls in Question2Answer when it is installed on Windows (IIS + Asp.Net MVC), until I discovered this setting. Thanks to Question2Answer developers :)

Go to Admin/Layout and set the base url like below:

Question2Answer - Admin/Layout Page

Now you are all set to run Question2Answer along with IIS Asp.Net MVC.

Gautam Jain
  • 6,789
  • 10
  • 48
  • 67