Redirection Rule Using web.config - asp.net

I want to redirect
http://www.mydomain.com/rakhi-blog/index.php/2012/06/20/my-article/ to
http://www.mydomain.com/rakhi-blog/2012/06/20/my-article/
For this I use the following code:
<configuration>
<system.webServer>
<defaultDocument>
<files>
<remove value="index.php" />
<add value="index.php" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:0}" />
</rule>
<rule name="Rewrite Index">
<match url="^index.php/*" />
<action type="Redirect" url="/rakhi-blog/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But This Code is not working. Can Any one Help Me Pls.

try:
<rewrite>
<rules>
<rule name="Rewrite Index">
<match url="^rakhi-blog/index.php/(.+)$" />
<action type="Redirect" url="/rakhi-blog/{R:1}" />
</rule>
</rules>
</rewrite>

In the past, I used the instructions in the article "Using Failed Request Tracing to Trace Rewrite Rules" to find and resolve such issues.
Basically, you turn on tracing and see what and why IIS is actually redirecting (or not redirecting). You then can see whether your expectations match the actual results.

Related

Use web.config to redirect HTTP to HTTPs

I have installed WordPress on my IIS server and I also have the SSL Certificate installed.
I have researched every thread I could find on this, but still couldn't get it work. I found this thread from a guy on Apache who is facing the same issue, but I am on IIS and don't know how to get it to work with IIS.
Similar to that thread, here is what is happening:
https://www.example.com is working great
https://example.com is redirecting to the above, also great!
Here is the problem:
http://www.example.com is still accessible, no good, as this should redirect to https://www.example.com
Also:
http://example.com redirects to http://www.example.com.
How can I fix this, so that it all redirects to https://www.example.com?
I am on IIS and here is what my web.config looks like:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="WordPress Rule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Thanks to Dan, this worked.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
<rule name="WordPress Rule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
First I would like to suggest you to get extension called “URL Rewrite” on your IIS.
Then you should match your code with the following code:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^1$" />
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/OWA/" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Removing index.cfm from url with web config

quick question -
Currently my urls look like this: index.cfm/camp/another-test
I would like for them to look like this: camp/another-test
I'm able to do this fine on apache with my .htaccess but I need to be able to do it on iis7 with the web.config. Here's my rewrite so far:
<rewrite>
<rules>
<rule name="Remove index.cfm" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" negate="true" pattern="^/(assets|files|miscellaneous|robots.txt|favicon.ico|sitemap.xml|index.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/index.cfm/{R:1}" />
</rule>
</rules>
</rewrite>
Thanks for the help!
I believe CFWheels requires that you route rewrite requests through rewrite.cfm not index.cfm.
See the comment by Chris Peters on this question
If you adjust:
<rewrite>
<rules>
<rule name="Remove index.cfm" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" negate="true" pattern="^/(assets|files|miscellaneous|robots.txt|favicon.ico|sitemap.xml|index.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/index.cfm/{R:1}" />
</rule>
</rules>
</rewrite>
to:
<rewrite>
<rules>
<rule name="ColdFusion on Wheels URL Rewriting" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" matchType="Pattern" ignoreCase="true" negate="true" pattern="^/(flex2gateway|jrunscripts|cfide|CFFileServlet|cfformgateway|railo-context|files|images|javascripts|miscellaneous|stylesheets|robots.txt|favicon.ico|sitemap.xml|rewrite.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/rewrite.cfm/{R:1}" />
</rule>
</rules>
</rewrite>
it should solve your problem, provided you have:
<cfset set(URLRewriting = "On")>
within /config/settings.cfm
Try adding this rewriting rule:
<rewrite>
<rules>
<rule name="ColdFusion on Wheels URL Rewriting" enabled="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{SCRIPT_NAME}" negate="true" pattern="^/(flex2gateway|jrunscripts|cfide|CFFileServlet|cfformgateway|railo-context|files|images|javascripts|miscellaneous|newsletters|stylesheets|robots.txt|favicon.ico|sitemap.xml|rewrite.cfm)($|/.*$)" />
</conditions>
<action type="Rewrite" url="/rewrite.cfm/{R:1}" />
</rule>
</rules>
</rewrite>

redirect issue on webconfig file

please check sfveinaesthetics.com/index.php opens but http://sfveinaesthetics.com/ dont.. its a wordpress site in iis7 server.
inner pages work http://sfveinaesthetics.com/about-us/ fine..
Please get a me solution asap.. web config is like
<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="index.php/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Not sure if it's causing the problem but the default rewrite rule for Wordpress does not include the /{R:0} part in the rewrite action. Normally it's:
<action type="Rewrite" url="index.php" />

IIS 7 URL Rewrite Module

I can see the Outbound URL Rewrite working, because it replaces all the URL's on the page with the new URL. So wherever I put this:
/post.asp?topic=question&id=123
it rewrites it to...
/question/123
But, when I visit the rewritten URL /question/123 I get a 404 error. It looks like the rewriting part works, but how about loading the URL once you goto the rewritten URL?
I used the 'Create Friendly URL' wizard in IIS > URL Rewriting and checked the Outbound and Redirect rule as well., just like this example explains: http://learn.iis.net/page.aspx/497/user-friendly-url---rule-template/
Here is the Web.Config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<remove name="RewriteUserFriendlyURL1" />
<remove name="RedirectUserFriendlyURL1" />
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^post\.asp$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^topic=([^=&]+)&id=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/post.asp?topic={R:1}&id={R:2}" />
</rule>
</rules>
<outboundRules>
<remove name="OutboundRewriteUserFriendlyURL1" />
<rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)/post\.asp\?topic=([^=&]+)&(?:amp;)?id=([^=&]+)&(?:amp;)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
Have you modified the pattern(s)? I've created rules with same steps using wizard. However my configuration file is different than yours, it works smoothly. Compare the patterns, especially for OutboundRewriteUserFriendlyURL1.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^post\.asp$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^topic=([^=&]+)&id=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="post.asp?topic={R:1}&id={R:2}" />
</rule>
</rules>
<outboundRules>
<rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)post\.asp\?topic=([^=&]+)&(?:amp;)?id=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>

HTTP to HTTPS redirect for only one URL

Was wondering if anybody could help. Thanks so much!
Problem:
blog.example.com
cool.example.com
redirect to https://www.example.com
Goal:
We're trying to only redirect
http://example.com
http://www.example.com
to https://www.example.com
Current code
Current code in web.config is this. Stuck on <match url="(.*)">
Below is rest of rewrite rule.
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to https">
<match url="(.*)" />
<conditions>
<add input="{SERVER_PORT}" pattern="443" negate="true" />
</conditions>
<action type="Redirect" url="https://www.example.com" />
</rule>
</rules>
</rewrite>
<modules runAllManagedModulesForAllRequests="true"/>
<defaultDocument enabled="true">
<files>
<clear/>
<!--Remove this line or place below to deprecate-->
<add value="default.aspx"/>
<add value="index.html"/>
<add value="index.php"/>
<add value="default.html"/>
</files>
</defaultDocument>
Modify <match url="(.*)" /> to include only example.com and www.example.com - right now you're matching all urls.
http://learn.iis.net/page.aspx/461/creating-rewrite-rules-for-the-url-rewrite-module/
Try this:
<rule name="Rewrite HTTP to HTTPS" stopProcessing="false" enabled="false">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" type=”Pattern” pattern="^(www\.)?example.com$">
</conditions>
<action type="Redirect" url="https://www.example.com{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />

Resources