An alternative way to rewriting URLs than the Rewrite URL graphic interface in IIS 7? - iis-7

Is there any way to do it, without the user interface?
I mean a non user interface way, something like a config file hosted in my web site, or some kind of alternative ?
Thank you very much!

YES, absolutely.
Your URL Rewrite rules should be stored in web.config file (unless it is explicitly forbidden which means they will be stored in IIS main config file -- for security and performance purposes, I guess).
Here is an example of such web.config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<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="SeeOther" />
</rule>
<rule name="CatchAll" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/catchall.php?page={REQUEST_URI}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

Webconfig URL Rewrite to Hide Classic ASP ext but not .aspx or other ext

I have been able to hide the .asp extension, but it also removes any other extensions and points to .asp
I know this is just a config issue but have no experience of the webconfig file configuration and wonder if anyone has a quick solution to save me a few hrs!
Code as I have it below from part of webconfig file
<rewrite>
<rules>
<rule name="Hide .asp Ext">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}.asp" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:0}.asp" logRewrittenUrl="true" />
</rule>
<rule name="Redirect .asp Ext" stopProcessing="true">
<match url="^(.*).asp" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="(.*).asp" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
</rules>
</rewrite>
Seems to me the first rule converts everything to an .asp extension first?
Try removing the first rule.
Try this? Seems to work on my test server.
<rule name ="redirect .asp to none" stopProcessing="true">
<match url="(.*)\.asp$" />
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
<rule name="hide .asp extension" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.asp" />
</rule>
You have two rules defined. It looks like the first matches on all requests and "rewrites" them to have a .asp extension. The second matches on on .asp and then performs a redirect of some kind.
What is not clear from your question is how you want to 'hide' these files. If you want to deny all requests to .asp you would be much better served adding a Request Filtering "File Extensions" https://learn.microsoft.com/en-us/iis/configuration/system.webserver/security/requestfiltering/fileextensions/index

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>

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" />

Url rewriting works on IIS but doesn't in localhost

I use url rewriting for my web site. I did settings on IIS and it works on server. But it doesn't work on localhost. It is normal because there is no page with rewrited url in my project files. How can I solve this problem? I use cassini server when I develop my project. Should I use local IIS in my computer? You can see here my url rewriting roles in web.config file:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<outboundRules>
<rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)ProductDetail\.aspx\?prid=([^=&]+)&(?:amp;)?product=([^=&]+)$" />
<action type="Rewrite" value="{R:1}ProductDetail/{R:2}/{R:3}/" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
<rules>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^urun/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="ProductDetail.aspx?prid={R:1}&product={R:2}" />
</rule>
</rules>
</rewrite>
<urlCompression doDynamicCompression="false" />
</system.webServer>
Why don't use Url Routing instead?
it's better way
Yes, you have to install IIS on your local computer using Add/Remove Windows Components.
Make sure also to enable the "URL Rewrite module" within your local IIS, once it's installed.
You need to add a negate condition <add input="{HTTP_HOST}" pattern="localhost" negate="true" />
so the URL rewriter ignores any requests on localhost.
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
</rules>
</rewrite>

URL rewriting in ASP.NET turning http://example.com/abcd into http://example.com/page.aspx?id=abcd

I have a problem in my web forms ASP.NET app where I would like to rewrite a url from
http://example.com/abcd
into
http://example.com/page.aspx?id=abcd
the abcd part will be unique and I cant create a folder for it
I want the users to always see the http://example.com/abcd url
would the solution be the same in Windows Azure?
Can somebody please help me with some hints?
Thank You!
In your web.config, in the system.webServer section put something like this:
<!-- This has been added to support url rewriting for ... -->
<rewrite>
<rules>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^Page\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^id=([a-zA-Z]+)$" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([a-zA-Z]+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Page.aspx?id={R:1}" />
</rule>
</rules>
</rewrite>
Please note that I have copied from the config we have in a production site and modified a little bit... needs testing.
The config should work when you have only words ([a-zA-Z]+), change the pattern to make it work for numbers.
hope it helps

Resources