Background : I am using IIS 7 (Windows 2008) and Zend Framework
What I am working on is rewriting a link site/blog?Id=1 to site/blog/1
Can anyone tell me why this doesn't work?
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^blog$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^Id=([^=&]+)$" />
</conditions>
<action type="Redirect" url="blog/{C:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^blog/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="blog?Id={R:1}" />
Thanks in advance.
Use this instead as mention in zend doc
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^.*$" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}"
matchType="IsFile" pattern=""
ignoreCase="false" />
<add input="{REQUEST_FILENAME}"
matchType="IsDirectory"
pattern="" ignoreCase="false" />
</conditions>
<action type="None" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^.*$" />
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
src
Try the following code in your .htaccess file:
RewriteRule ^blog/([0-9]+)/?$ blog?id=$1 [NC,L] # Handle requests
Related
I'm working on an MVC project, and I have the following working rule in my web.config, but it's redirecting all the subdomains to https. What I would like to do is to exclude some specific subdomains. For example, I need to redirect "mywebsite.com" and "www.mywebsite.com", but not "test.mywebsite.com" or "beta.mywebsite.com". The excluded subdomain should remain as "http://test.mywebsite.com" and "http://beta.mywebsite.com" without being redirected to https. How can I do that? Here's my rule in web.config:
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{REQUEST_URI}" negate="true" pattern="^/\.well-known/pki-validation/(.*)$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="true" />
</rule>
I used to have a similar configuration had used a rule along this line:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="redirect to https2" stopProcessing="true">
<match url="^" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^subdomain\.example\.com$" negate="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="false" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{URL}" redirectType="Found" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The rule below does the following:
- http://www.example.com -> https://example.com
etc
- https://example.com/contacts.html -> . https://example.com/contacts
So (all requests to HTTPS, and all www -> (non)www) + hides .HTML extensions.
I would like to add the ability to remove the following issue:
https://example.com/Home.html -> https://example.com/Home
But for the specific case of /Home I would prefer it displays /
Any help would be greatly appreciated..
Rule below:
<rewrite>
<rules>
<rule name="Force non-WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Redirect .html extension" stopProcessing="false">
<match url="^(.*)\.html$" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="(.*)\.html$" ignoreCase="false" />
</conditions>
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="hide .html extension" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.html" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.html" />
</rule>
</rules>
</rewrite>
I made some change in your rule to prevent rewrite for /Home and /. Then created two rules to handle request to /Home.html.
It works fine on my side and should achieve your requirement.
<rewrite>
<rules>
<rule name="Force non-WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Redirect .html extension" enabled="true" stopProcessing="true">
<match url="^(.*)\.html$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{URL}" pattern="(.*)\.html$" ignoreCase="false" />
<add input="{URL}" pattern="^/Home.html$" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="hide .html extension" enabled="true" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.html" matchType="IsFile" />
<add input="{URL}" pattern="^/$" negate="true" />
<add input="{URL}" pattern="^/Home$" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.html" />
</rule>
<rule name="redirect /Home" enabled="true" stopProcessing="true">
<match url="^Home.html$" />
<action type="Redirect" url="/" />
</rule>
<rule name="rewrite Home" stopProcessing="true">
<match url="^$" />
<action type="Rewrite" url="/Home.html" />
</rule>
</rules>
</rewrite>
I am trying for hours to get the following IIS rewrite to work:
Incoming URL:
https://example.com/services/control/status?Id=SO1234567
Expected output:
https://example.com/orders/detail/SO1234567
IIS Rewrite Rule:
<rule name="Custom rewrite rule" patternSyntax="ExactMatch" stopProcessing="true">
<match url="^services/control/status\?Id=(SO|so){1}([0-9]*)" />
<action type="Rewrite" url="/orders/detail/{R:0}" appendQueryString="false" logRewrittenUrl="true" />
</rule>
Additional information:
I am running a single web site in IIS.
1 web applications for an API under this web site.
Angular files within the web site.
Angular rules:
<rule name="Angular Routes" stopProcessing="false">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<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="/" />
</rule>
Complete Rewrite section:
<rewrite>
<rules>
<clear />
<rule name="Angular Routes" stopProcessing="false">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(marketplace)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
<rule name="Custom rewrite rule" patternSyntax="ExactMatch" stopProcessing="true">
<match url="^services/control/status\?Id=(SO|so){1}([0-9]*)" />
<action type="Rewrite" url="/orders/detail/{R:0}" appendQueryString="false" logRewrittenUrl="true" />
</rule>
</rule>
</rules>
</rewrite>
As far as I know, the url pattern will not match the query string. If you want to get the query string value, you should use rewrite condition.
Details, you could refer to below rule:
<rule name="QueryStringRue" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="^Id=([0-9a-zA-Z]*)" />
</conditions>
<action type="Redirect" url="https://example.com/orders/detail/{C:0}" />
</rule>
I have a webconfig file that already contained redirects and rewrites for friendly URL's. However, when I added an HTTP to HTTPS redirect, the URL's that used those friendly URL redirects and rewrites, break.
Result in Firefox..."Page is not redirecting properly"
Result in Chrome..."This page has a redirect loop"
Can anyone tell me why the HTTP to HTTPS redirect is causing this? And, how can I re-code this to work?
Here is my webconfig file...
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<connectionStrings>
<add name="" connectionString="" />
</connectionStrings>
<system.web>
<compilation targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<customErrors mode="Off" />
</system.web>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
<system.webServer>
<urlCompression doStaticCompression="false" doDynamicCompression="false" dynamicCompressionBeforeCache="false" />
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/errors/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" />
</rule>
<rule name="Redirect to https" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^ON$" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="Redirect-asp-aspx-IIS-new">
<match url="(.*?)asp$" ignoreCase="true" />
<action type="Redirect" url="{R:0}x" redirectType="Permanent" />
</rule>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^viewcategory\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^cat=([^=&]+)$" />
</conditions>
<action type="Redirect" url="viewcategory/{C:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^viewcategory/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="viewcategory.aspx?cat={R:1}" />
</rule>
<rule name="RedirectUserFriendlyURL2" stopProcessing="true">
<match url="^viewseries\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^cat=([^=&]+)&class2get=([^=&]+)$" />
</conditions>
<action type="Redirect" url="viewseries/{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^viewseries/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="viewseries.aspx?cat={R:1}&class2get={R:2}" />
</rule>
<rule name="RedirectUserFriendlyURL3" stopProcessing="true">
<match url="^viewproducts\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^series=([^=&]+)$" />
</conditions>
<action type="Redirect" url="viewproducts/{C:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL3" stopProcessing="true">
<match url="^viewproducts/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="viewproducts.aspx?series={R:1}" />
</rule>
</rules>
<outboundRules>
<rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)viewcategory\.aspx\?cat=([^=&]+)$" />
<action type="Rewrite" value="{R:1}viewcategory/{R:2}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)viewseries\.aspx\?cat=([^=&]+)&(?:amp;)?class2get=([^=&]+)$" />
<action type="Rewrite" value="{R:1}viewseries/{R:2}/{R:3}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL3" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)viewproducts\.aspx\?series=([^=&]+)$" />
<action type="Rewrite" value="{R:1}viewproducts/{R:2}/" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<defaultDocument>
<files>
<add value="index.aspx" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
Use this
`<rule name="HTTP to HTTPS" enabled="true" patternSyntax="Wildcard"
stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"
redirectType="Found" />
</rule>`
I'm using 302 redirect from external domain:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="https://www.example.com" httpResponseStatus="Found">
<clear />
</httpRedirect>
</system.webServer>
</configuration>
After the redirect the new domain look like this: https://www.example.com/,http://www.example.com
This the web.config of the target domain:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="false" />
<defaultDocument>
<files>
<clear />
<add value="index.htm" />
<add value="index.html" />
<add value="default.asp" />
<add value="default.aspx" />
<add value="index.php" />
<add value="index.asp" />
<add value="home.aspx" />
</files>
</defaultDocument>
<rewrite>
<rules>
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{URL}" pattern="^system.*" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="/index.php?/{R:1}" appendQueryString="false" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{URL}" pattern="^application.*" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="/index.php?/{R:1}" appendQueryString="false" />
</rule>
<rule name="Imported Rule 3" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?/{R:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I've tried many versions of 302 redirect, but it seems that the url doubled in the target domain.. what would make it happen?