URL Rewrite Map and Wordpress - wordpress

I am working on a migration from ASP.NET to Wordpress and I want to put 301 redirect rules in place using a Rewrite Map with URL Rewrite in IIS 7.5. I have all the redirects in a file named rewritemap.config, and I've created the redirect rule in URL Rewrite in IIS, but I can't get the redirect to work with Wordpress. I don't know if this is possible with Wordpress having it's own rewrite rule in the web.config as well to send everything to index.php where it handles it's own rewrites internally as explained in this link:
How does Wordpress rewrite the URL without a rewrite map?
The XML for the web.config is below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps configSource="rewritemap.config" />
<rules>
<rule name="Redirect old links from redirect map" stopProcessing="true">
<match url=".*" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
<rule name="wordpress" enabled="true" patternSyntax="Wildcard">
<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="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The XML for the rewritemap.config file was created by IIS Admin so I expect that it is correct and it matches the format I've seen on other blog posts that have shown how to do this. Am I attempting something impossible with this? I have hundreds of old aspx pages I need to 301 redirect to specific links in the Wordpress site and there are to many external references to the old links to even think about not having the 301 redirects in place.

Related

How do I negate login.aspx in the web.config with Wordpress

We have recently created a Wordpress site which will be hosted on a Windows 2012 Server. The server is currently serving compiled code of which one of the URL's it uses is login.aspx.
When we add the Wordpress site and enable permalinks, we get the default Wordpress page instead of login.aspx.
We have unsurprisingly narrowed it down to the web.config rewrite rule.
<rewrite>
<rules>
<rule name="wordpress" patternSyntax="Wildcard">
<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>
I'm sure we have to negate the login.aspx page some how but I'm not sure how to do it. This has to work as a single site for now.
All I needed to do was add in the following:
<add input="{URL}" negate="true" pattern="*.aspx" />

Redirect Non-WWW to WWW (except one spesific subdomain)

On my Windows 2012 server I have installed the URL Rewrite module in IIS. I have followed this guide to redirect non-www to www in web.config:
http://www.surfingsuccess.com/asp/iis-url-rewrite.html#.VF6GBid0yAU
So far, so good!
The only problem is that we also host the subdomain "api.example.com". This API stops working when I apply the code in web.config.
My question is: What is wrong with the code below? Why does the subdomain stop working when I try to redirect non-www to www, except "api.example.com"?
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
<add input="{HTTP_HOST}" pattern="^api\.example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
I have similar check and I have same rule but have specified differently in pattern.
My suggestion is to enable Failed request tracing to check URL rewrites.
http://www.iis.net/learn/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules
Edit: Updated rule.
<rewrite>
<rules>
<rule name="non-root" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(www.example.com)$" negate="true" />
<add input="{HTTP_HOST}" pattern="^(api.example.com)$" negate="true" />
<add input="{HTTP_HOST}" pattern="^(example.com)$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
Actually, there was nothing wrong with my rewrite code. The reason why the API stopped working was because it had a referance to the top domain of the site (example.com), this caused a conflict with the rewrite code. After changing the referance to the www -version of the doman (www.example.com) everything worked fine.

How can I create a URL rewrite rule for a domain-only request?

I am trying to create a URL rewrite for a site that has multiple domains. For example one of the domains is mydomain.com and if a user puts www.mydomain.com in their browser and does not specify a page I want to rewrite the URL to call www.mydomain.com/landingpage.aspx?cat=1&sol=4.
If the user calls anything else such as www.mydomain.com/somepage.aspx this rule should be ignored.
I have installed URL Rewrite 2.0 on the server 2008 R2 machine we have and I have added this rule to the web.config.
<rewrite>
<rules>
<rule name="mydomain.com" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain.com" />
<add input="{PATH_INFO}" pattern="^$" negate="true" />
</conditions>
<action type="Rewrite" url="\landingpage.aspx?cat=1&sol=4" />
</rule>
</rules>
</rewrite>
I am using the {PATH_INFO} of ^$ so that if anything other than just a call for the domain occurs this should ignore it I think. However it does not work.
I am using .NET 4.0 on the site.
Can someone tell me what I am doing wrong please?
You are looking for following rule:
This will check if URL is empty i.e. no page is specified using match url=^$ - empty string and redirect to specific page.
<rule name="Redirect to specific page" enabled="true" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain.com$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/landingpage.aspx?cat=1&sol=4" />
</rule>

ASP.NET - rewriting all https requests to http

My issue is precisely the one presented here, and I've decided to try rewrite all https requests to http. I've searched long and hard but there doesn't seem to be a definitive way to achieve this - see these questions (no solutions): Redirect https to http using rewrite rule in webconfig file ; https://stackoverflow.com/questions/15214717/iis-rewrite-https-to-http-whilst-keeping-existing-https-rules
I've added the rewrite module to IIS, and tried the following in web.config:
<rewrite>
<rules>
<clear />
<rule name="force http" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But it still allows the user to access a non-https site with https (essentially accessing a different site).
How do I force all https requests to be http requests?
edit: I've also tried every suggested solution here with no luck. The url rewrite module is definitely successfully installed on IIS!
edit2: Tried the following without success:
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="force http" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern="^(?:www)?\.test.site\.com$"
negate="true" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
I restarted IIS and the rewrite rules reflect in inetmgr. Loading https://test.site.com/ still loads with https.
A couple of things. First the rewrite needs to process when HTTPS is on and not off. Second, for the application that needs to run over HTTPS you will need to exclude it from the rewrite. The revised rewrite rule should look something like this:
<rewrite>
<rules>
<clear />
<rule name="force http" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern="^example\.com$"
negate="true" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
This should keep https://example.com/login on https and all other URL's will get redirected to http. For example, https://test.example.com/login will be redirected to http://test.example.com/login. This rewrite rule needs to be placed on the site with the HTTPS binding for the rewrite to work properly.
Please be aware when using a 301 permanent redirect some browsers won't make the request out to the server on subsequent hits so after changing the rule a browser cache clear is required. The network tab may even lie and say the request is made but an external tool like Fiddler or Wireshark will let you know for sure.

How to tell IIS7 to redirect traffic to asp.net

I have a url rewrite routine in global.asax, when the user types:
http://example.com/products/category/electronics
the server serves:
http://example.com/products.aspx?category=45
the problem is that IIS7 does not send this request to be handled by asp.net because the requested path has no folder in the directory structure.
How can I tell IIS to let this request be handled by asp.net without creating a directory for each product and category?
Technically you don't have IIS do this, you use URL Re-Writing to do it. This is a normal feature of ASP.NET MVC. However, it's possible in "classic" form-based asp.net as well.
There's an article about it here: http://msdn.microsoft.com/en-us/library/ms972974.aspx
Listed in the article are a number of ways to accomplish this, as well as a detailed explanation of writing an httpModule.
As a matter of fact, the article shows how to do EXACTLY what you're asking. Search on the page for "when a user visits /Products/Beverages.aspx" on the page and it'll take you right to the demonstration of how it works. (Instructions are higher up in the article.)
In a nutshell don't do this in your Global.asax. Use the IIS7 Url Rewriter instead:
Using the URL Rewrite Module
You have one of two solutions -
Use a URL Rewrite Map and map every category name to a category ID:
<rewrite>
<rewriteMaps>
<rewriteMap name="StaticRewrites">
<add key="/products/category/electronics" value="/products.aspx?category=45" />
<add key="/products/category/books" value="/products.aspx?category=46" />
<add key="/products/category/dvds" value="/products.aspx?category=47" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite Rule 1 for StaticRewrites" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="False"/>
</rule>
</rules>
</rewrite>
Use a more "dynamic" rewrite:
<rewrite>
<rules>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^products\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^([^=&]+)=([^=&]+)$" />
</conditions>
<action type="Redirect" url="products/{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^products/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="products.aspx?{R:1}={R:2}" />
</rule>
</rules>
</rewrite>
In the above case you'd need to change your products.aspx page to accept the category name, then in that page look up the actual integer ID of the category in the database.
Passing the rewrite responsibility to IIS7's URL Rewrite module (or any other rewriter such as Iconics IIRF or HeliconTech's ISAPI Rewrite) means that you can change these rules separately rather than burning them into your code. Burning these rules into your code means you have to redeploy every time you make a change.

Resources