How to rewrite query in IIS 7 - html

I want to rewrite this URL
https://www.website.com/parameter-1?query=100345046
To this
https://www.website.com/parameter?query=100345046
Basically I want the -1 removed from the URL.
I made an example which works, using RegExr, but I don't know how to implement this using the IIS.
So far I made this rule but I can't get the query part after the -1 to stick the to rewrote URL.
<rule name="Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="^/parameter-1$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/parameter" appendQueryString="true" />
</rule>

Alright I figured it out.
<rule name="Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="/parameter-1\?query=(.*)$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/parameter?" appendQueryString="true" redirectType="Permanent" />
</rule>

Related

How to force site to use https only

I'm tryin to force my website to only accept https, by adding the following to my webconfig:
<clear />
<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" appendQueryString="false" />
</rule>
This works, if you load the site using http it now re-directs the site to https, but every time I try to go to a different page or submit a form I get a:
405 - HTTP verb used to access this page is not allowed.
This currentle my webconfi:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="PW_BADPOST" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" />
<add input="{HTTP_REFERER}" pattern=".*\/wp-admin.*" />
</conditions>
<action type="CustomResponse" statusCode="404"/>
</rule>
<rule name="PW_BadURL" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="\.php" />
<add input="{REQUEST_URI}" pattern="die\(" />
<add input="{REQUEST_URI}" pattern="connector\.asp" />
</conditions>
<action type="CustomResponse" statusCode="404"/>
</rule>
<!-- directories to ignore -->
<rule name="PW_IgnoreDir" stopProcessing="true">
<match url="^(css|img|images|js|tools)\/(.*)$" ignoreCase="true" />
<action type="None" />
</rule>
<!-- Pages -->
<rule name="PW_Pages" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?{R:1}" />
</rule>
</rules>
</rewrite>
<directoryBrowse enabled="false" />
</system.webServer>
</configuration>

Url Rewrite to new url

<rule name="WC2018" stopProcessing="true">
<match url="^product/sb/sales.htm" />
<action type="Rewrite" url="product/sb/market/sales.htm" />
</rule>
Above is the url rewrite that i do, the original link is
product/sb/sales.htm
I need to rewrite it to
product/sb/market/sales.htm
But it does not work. I not sure what went wrong.
You can try this rules :
<rule name="rule 1k" stopProcessing="true">
<match url="^product/sb/market/sales.htm$" ignoreCase="true" />
<action type="Rewrite" url="/product/sb/sales.htm" />
</rule>

hide/remove url extension in windows servers

I read that .htaccess doesn't work on windows server like godaddy. So to hide or remove extensions like .php and .html on URL, you can accomplish this by creating a web.config file with the following code:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RewriteHTML">
<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="{R:1}.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I tried to make web.config file and save it where index.php is with that code but nothing happened.. I tested it with the like this
<ul>
<li>
Attack on Titan Episode 3
</li>
</ul> with a href of "attackontitan-3"
I want url to be http://mysite/watching/attack%20on%20titan/attackontitan-3 not http://mysite/watching/attack%20on%20titan/attackontitan-3.php
Use rewrite to add the extension, like this:
<rule name="rewritephp">
<!--Removes the .aspx extension for all pages.-->
<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="{R:1}.php" />
</rule>
Or This tutorial does a great job at explaining what you need to do with IIS.
http://atlantawebsites.blogspot.com/2010/06/vanity-urls-with-godaddy-hosting-using.html
You need to add a Rewrite and Redirect rules. The following will redirect and rewrite page.php to just page. If you want to do the same for html, just add those rules.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<!-- Remove the existing rules just incase we already defined them -->
<remove name="RedirectPhpExtensions" />
<remove name="RewritePhpExtensions" />
<!-- Add a rule to redirect x.php pages to just x -->
<rule name="RedirectPhpExtensions" enabled="true" stopProcessing="true">
<match url="(.*)\.php$" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<!-- Add a rule to rewrite the x pages to x.php behind the scene -->
<rule name="RewritePhpExtensions" enabled="true" stopProcessing="true">
<match url="(.*)" negate="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Place this web.config in the root directory of your domain.
Then you need to restart the Application Pool on godaddy's site:
Web Hosting > Manage > IIS Management > Recycle App Pool button

Exceptions in redirection in URL in IIS Rewrite module are ignored

I have a new site https://www.NewSite.com and I would like all the traffic to my old site https://www.OldSite.com to be redirected to the root of my new site https://www.NewSite.com/, with some exception.
For instance I would like the URL (among others) https://www.OldSite.com/noredirect not to be redirected and others to be rewritten. For instance:
https://www.OldSite.com -> https://www.NewSite.com/
https://www.OldSite.com/library->https://www.NewSite.com/
https://www.OldSite.com/kb/ -> https://www.newsite.com/kb
https://www.OldSite.com/kb/articles.aspx?id=45 -> https://www.NewSite.com/kb
https://www.OldSite.com/noredirect -> https://www.oldsite.com/noredirect
https://www.OldSite.com/noredirect/page.aspx -> https://www.oldsite.com/noredirect/page.aspx
Therefore I wrote two rules in my Old Site site in IIS:
<rewrite>
<rules>
<clear />
<rule name="Rewrite To New Site" stopProcessing="true">
<match url="/kb(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="https://www.newsite.com/kb" />
</rule>
<rule name="Redirect to New Site" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="(.*)/noredirect$" negate="true" />
</conditions>
<action type="Redirect" url="https://www.newsite.com" appendQueryString="false" />
</rule>
</rules>
</rewrite>
This rules just redirect all the requests to my old site to the root of my new site, ignoring the exceptions and the rewrite rule. The same happens with:
<rule name="Redirect to New Site" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_URI}" pattern="^/noredirect(.*)" negate="true" />
</conditions>
<action type="Redirect" url="https://www.newsite.com" appendQueryString="false" />
</rule>
What am I doing wrong?
Make sure you've checked "Enable proxy" under Application Request Routing cache.
You can find some information about it here:
http://www.iis.net/learn/extensions/configuring-application-request-routing-%28arr%29/creating-a-forward-proxy-using-application-request-routing
Good luck!

How to add multiple URL rewrite rules in a web.config

How to add multiple URL rewrite rules in a web.config
I want to match any url that contains "sales" and "registrationsuccess".
I get errors with when I try the following:
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="(.*sales*)"/>
<match url="(.*registrationsuccess*)"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
</rule>
</rules>
</rewrite>
This is how you add more than one rule:
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS (Sales)" stopProcessing="true">
<match url="(.*sales*)"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
</rule>
<rule name="Redirect HTTP to HTTPS (RegistrationSucces)" stopProcessing="true">
<match url="(.*registrationsuccess*)"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
</rule>
</rules>
</rewrite>
But for what you're trying to do, you can do it with one rule, like below:
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="ON" />
<add input="{PATH_INFO}" pattern="^(.*)/sales/(.*)" />
<add input="{PATH_INFO}" pattern="^(.*)/registrationsuccess/(.*)" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>