IIS Rewrite even rewrites images/css/js - results in 404 not found - asp.net-mvc

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}&{QUERY_STRING}" appendQueryString="false" />
</rule>
Please tell me how to fix this. I am using Asp.Net MVC (if it matters).

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}&{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}&{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:
Now you are all set to run Question2Answer along with IIS Asp.Net MVC.

Related

IIS Rewrite conditions rule is not working

First thankyou anyone , Engilsh is not my native language
But this problem is very difficult for me
I need
http://xxxx.show.bis.tw/event_register/index.php?id=image01
to
http://xxxx.show.bis.tw/event_register/image01
and if the users enter http://xxxx.show.bis.tw/event_register/image01 on address bar
the website is can work
I think the rules is
users can input those
http://xxxx.show.bis.tw/event_register/image01
http://xxxx.show.bis.tw/event_register/index.php?id=image01
1.
http://xxxx.show.bis.tw/event_register/index.php?id=image01
Redirect like this http://xxxx.show.bis.tw/event_register/image01 and go to rule2
Rewrite <action type="Rewrite" url="index.php?id={C:1}" />
2.
http://xxxx.show.bis.tw/event_register/image01
just only Rewrite <action type="Rewrite" url="index.php?id={C:1}" />
my code is
if file and directory do Redirect or Rewrite website will broken (css/js/image)
I try to rule>1 and rule>2 write in one rule but it not work
I don't know how to fix it
<rewrite>
<rules>
// 1
<rule name="025" stopProcessing="true">
<match url=".*" />
<conditions> // file and directory not Redirect|Rewrite
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="{C:0}" appendQueryString="false" />
</rule>
// 2
<rule name="026">
<match url="index\.php(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="id=(.*)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
// 3
<rule name="027" enabled="true" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions>
<add input="{URL}" pattern="/index.php?id=" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?id={R:1}" />
</rule>
</rules>
</rewrite>
thankyou anyone very thank
I almost tow days can't not sleep just fix this
Have you tried rewritemaps instead of rules in your web.config file?
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="rewrites">
<add key="/event_register/image01" value="/event_register/index.php?id=image01" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
</configuration>

IIS web.config : Rewrite URL file as subdomain

I am trying to rewrite my url with a web.config file.
Actual URL: sudomain.mydomain.com/legal.html
Wished URL: legal.mydomain.com
I have already hide the html extension.
<rule name="Hidehtml" enabled="true">
<match ignoreCase="true" url="Legal" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="Legal.html"/>
</rule>
I saw this kind of rewrite is possible with subfolder, but I wonder if it is possible with a file.
I am trying this one but it give me a DNS error.
<rule name="testrule" stopProcessing="true">
<match url="legal(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="mydomain.com" />
</conditions>
<action type="Redirect" url="http://legal.mydomain.com" />
</rule>
I wonder if I am not missing something, maybe a redirect is needed too?
Thanks for your help !
To achieve this,
I just registered two CNAME for my website.
FQDN:subdomain.mydomain.com
FQDN:legal.mydomain.com
Then I apply them to my IIS site.
Just make sure both of them can be accessed in web browser.
Then I apply following rewrite rules
<rules>
<rule name="testrule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^/legal\.[a-zA-Z]+" />
<add input="{HTTP_HOST}" pattern="subdomain.candy.com" />
</conditions>
<action type="Redirect" url="http://legal.candy.com" />
</rule>
<rule name="rewrite rule">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="legal.candy.com" />
<add input="{URL}" pattern="^(/)?$" />
</conditions>
<action type="Rewrite" url="http://subdomain.candy.com/legal.html" />
</rule>
</rules>
When you access sudomain.mydomain.com/legal.html, the request will be redirected to legal.mydomain.com/ and legal.mydomain.com will rewrite back to /legal.html for response content.
Please keep in mind that legal.mydomain.com is always required to be registered in DNS even you just use it as a mask.
If your website are forwarding the public internet, please remember to purchase domain for legal.domain.com as well.

MVC route to a Angular app in a virtual folder

I have an Angular running inside a MVC app. It is set up as a Virtual folder inside the Web application on the IIS.
Currently I have a IIS rewrite rule so the Routing is handled by the Angular app. But this is causing some other problems so I like to find out if it can be done with the MVC routing?
So everything at ../app/[pathandquery] should be routed to the app/[pathandquery] folder
add this to webconfig
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>

Temphire (Angular) does not support deep links and refresh?

When playing with the Temphire app, I noticed that when I try to refresh a page by hitting F5, the application shows a blank page ! (ngViewport is not rendered)
Found the solution... it was an IIS issue, needed to rewrite urles in order to keep it working :
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>

URL Rewrite on IIS from http to https is not working,

I have a problem. On IIS I got a web-site with two ports 80 and 443(https). I want to redirect all the http requests from user to https. I also added Rewrite rule to https, but when I enter in browser http://localhost/site it gives me the same page. I need to redirect user to httpS://localhost/site.
Maybe this is because of my local configurations?
And I disable Require SSL on IIS.
The rule is:
<rewrite>
<rules>
<rule name="HTTPS Redirect">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
</rules>
</rewrite>
Thank you.
Below is the exact rule we use on a production IIS 7 site to redirect all request from HTTP to HTTPS
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
There are some minor differences between what you have posted and what we use. Also, since you are running on local host, you wouldn't be using the built-in web server with visual studio would you? I don't think it will process IIS rewrite rules.
I realize this may not be your issue, however I had a similar debacle that was cause by a different problem.
I had enabled Require SSL and that caused the site to continually return a 403. So to use this method it appears you must disable SSL Settings -> Require SSL.
Hope this helps someone.
Also, if you have more than one rule, order could matter. Be sure to add the redirect rule before other rules or redirect may not fire.
Here is an example that is using SPA that required the rule order.
<rules>
// Adding this as last rule will cause it to not redirect
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="static dist files" stopProcessing="true">
<match url="^(.+)" />
<conditions>
<add input="{APPL_PHYSICAL_PATH}app\{R:1}" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="/app/{R:1}" />
</rule>
<rule name="index.html as document root" stopProcessing="true">
<match url="^$" />
<action type="Rewrite" url="/app/" />
</rule>
<rule name="SPA Routes" stopProcessing="true">
<match url=".*|.*/.*$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/app/" />
</rule>
</rules>
It works for me in IIS 8.5
redirect
Three points:
Use single word OFF rather than ^OFF$
Use url="https://{HTTP_HOST}/{REQUEST_URI}"
Use redirectType=Permanent rather than Found although both are working but preferable Permanent type in this case
The webconfig code
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
you can add this to your pages. to force redirection.
if (!Request.IsSecureConnection)
{
Uri uri = new Uri(Request.Url, Request.RawUrl);
Response.Redirect(string.Format("https://{0}{1}{2}", uri.Host.StartsWith("www.x.com") ? uri.Host : "www.x.com", uri.AbsolutePath, uri.Query));
}
I just seen your tag of mvc
add this attribute to your actions. or controllers.
[RequireHttps]

Resources