I'm running an Asp.Net (4.0) website. There also a Wordpress blog on the same machine.
The blog is a folder under the main domain: www.mydomain/blog/
Here is the problem.
I've just been doing some page analysis and discovered that link juice is being split between pages with a trailing slash and pages without a trailing slash.
I found the code to remove the trailing slash (my preferred option) and added it to my web config - it which works on the main site but causes problems with Wordpress.
<rule name="Remove trailing slash">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
The code isn't the problem. Asp.Net routing in (4.0) creates routes without a trailing slash - Wordpress on the other hand, creates routes with a trailing slash - therefore implementing the code in my main web.config causes a loop problem. I'm also worried about the SEO affects.
Is there a way I can modify the above code so that it kicks in for the main site and not for the blog?
Thanks in advance.
Try adding this line of xml into the conditions:
<add input="{REQUEST_FILENAME}"
matchType="Pattern"
pattern="/blog/$"
negate="true" />
It should tell the redirect to not fire if it ends in /blog/
Related
Hi have two problems using IIS rewrite and redirect rules in front of a web page hosted on GitHub Pages.
Rewrite to GitHub Page
Update: Everything worked perfectly. As described later in this question, it was GitHub Pages that did the redirect to the canonical URL. Adding a rewrite appending the missing trailing slash before hitting GitHub Pages fixed it:
<rule name="Add Trailing Slash" stopProcessing="true">
<match url=".*[^/]$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" pattern=".+?\.\w+$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{ToLower:{R:0}}/" />
</rule>
The site is hosted on GitHub Pages and available here: http://elmahio.github.io/blog/.
In order to rewrite requests to my proxy, I have added the following rewrite rule:
<rule name="Proxy" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" negate="true" pattern="^(.*)/.well-known/(.*)$"/>
</conditions>
<action type="Rewrite" url="http://elmahio.github.io/blog/{R:1}" />
</rule>
The rewrite rule works like a charm. But if anyone forget the trailing slash to an URL, it seems to redirect to the GitHub Page version. Example:
https://blog.elmah.io/great-dot-net-conferences-to-attend redirects to http:// elmahio.github.io/blog/great-dot-net-conferences-to-attend/
I can see that GitHub Pages automatically append the trailing slash if not there. This might even be the main problem, since my proxy rewrite to the GitHub one and then redirect to append the trailing slash.
Any ideas to how to fix this? Maybe add a redirect rule in my proxy, appending that slash before rewriting to GitHub Pages?
Redirect to HTTPS
Update: Removing my custom redirect rule and enabling HTTPS Only on the Custom domain view on Azure, fixed this problem
I want all requests made to a non-https URL, redirected to HTTPS. I have added a redirect rule to web.config like this:
<rule name="RedirectToHTTPS" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent"/>
</rule>
The rewrite rule works fine on URL's containing a path. Example:
http://blog.elmah.io/great-dot-net-conferences-to-attend/ redirects to https://blog.elmah.io/great-dot-net-conferences-to-attend/
But when visiting the front page (http://blog.elmah.io/ or http://blog.elmah.io) without HTTPS, I am not redirected to the HTTPS URL.
What could go wrong here?
All your rewrites are correct. Most probably issue exists because of GitHub pages. Try to do the following:
1) Create a rule to append trailing slash. You can find the example of rewrite rule in this thread IIS URL Rewrite: Add trailing slash except for .html and .aspx
2) Instead of using rewrite rule for redirect to HTTPS, you can enable HTTPS Only on the Custom domain view on Azure
I'm working on my Wordpress production site and I'm having a strange error that doesn't allow me to add or edit any Link which points to an external URL (not on my domain). Originally I could do that, as I managed to submit many links for my site before that.
Going through the Links menu I can open any of the existing Links, but when I press the Update button, a blank page with the 404 error page appears. If I do the same with a link on my domain, it works perfectly!
I have researched on this subject but could not find anything similar. I'm really frustrated.
Any help is much appreciated!
My environment:
Working with WP 3.5.1, PHP 5.3.6
Using Permalinks custom: /%postname%
Windows 2012 Server with IIS 8 and URL rewrite (no .htaccess)
Rules I'm using:
<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>
I am unable to remove the trailing slash my site's URLs even with the URL rewrite from: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/.
Frustrating really since it should be so simple but my attempts have not produced any results.
I even went as far as to create a test directory and added file called desktops.aspx and and sub folder called desktops.
without the sub directory "/test/desktops" loads fine since i set the default document to look at desktops.aspx.
with a subfolder created and still referencing "/test/desktops" it forces the slash and looks at the sub directory.
Why does IIS does this since its supposed to look for the file first then the sub directory correct? Are there any settings on the server side that would force back the slash?
URL Rewrite Snippet:
<rule name="SEO - Remove trailing slash" stopProcessing="false">
<match url="(.+)/$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_METHOD}" pattern="GET" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="_{R:1}" />
</rule>
any help would be welcome
You are using an action of type Rewrite but you want a Redirect.
Change your configuration to:
<rule name="SEO - Remove trailing slash" stopProcessing="false">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
You also need to change url="(.+)/$" to url="(.*)/$".
TIP:
The best way to test your pattern is to use the IIS test pattern tool.
At the root of your website -> URL Rewrite -> Create a blank rule -> click on test pattern:
I was having this same problem and here is what I found.
My intent was to use this rule on an MVC website but I didnt want to test in production so I tested the rule on a site already setup which happened to be web forms asp.net.
I encountered the same problem as you. Navigating to www.example.com/test redirected to www.example.com/test/ even with the rule in place.
So I noticed the conditions to check if the requested url is a file or directory and I removed them.
Now going to www.example.com/test/ redirected to www.example.com/test. Yay! No. IIS automatically added another redirect back to www.example.com/test/ resulting in a redirect loop. Boo.
I then found this article https://support.microsoft.com/en-us/kb/298408 which relates to IIS 6 but is obviously still an issue.
So because I am in an asp.net web forms site, in my case /test is a physical directory and there is something in IIS that forces the trailing slash for directories. And sorry to say but I couldn't find a way to easily turn it off.
However! My requirement was for MVC and configured routes that are NOT directories. So I tried it again on a MVC website and the redirect to remove the trailing slash worked perfectly.
The rule I ended up with is:
<rule name="RemoveDatSlash" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
Hope this helps!
The problem I had were links to PDF files having trailing forward slashes, so what worked for me in Windows Server 2008 R2 running IIS 6.1:
Click on the website that needs the rule and in the Features view open up URL Rewrite and then on the Actions section (right pane) choose Add Rule(s) and select Append or Remove the trailing slash symbol. In the next window on the drop down choose remove if exists.
Hope this helps.
I have a ASP.NET MVC3 website located at http://mydomain.com/mymvcapp/. However, the root of the webiste (mydomain.com) contains a WordPress site running PHP. Therefore, I put the following IIS URL Rewrite rule to allow WordPress to function correctly via its rewriting mechanisms:
<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>
So with this rule in place my WordPress functions perfectly, however, my MVC rewriting does NOT work. How would I alter this rule to allow both WordPress and MVC (under the /mymvcapp/ folder) to coexist nicely?
Figured it out on my own. Regex is probably one of the most powerful YET complicated / confusing technologies there is. But in this case the patternSyntax flag was set to Wildcard, not Regex, which caused my confusion. Hope this helps someone else out there! =]
<rewrite>
<rules>
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*" />
<conditions logicalGrouping="MatchAll">
<add input="{PATH_INFO}" pattern="/mymvcapp/*" negate="true" />
<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>
This is one of the very few posts anywhere that talks about making WordPress and ASP.NET coexist nicely in an IIS setup. So Kudos for that.
I was thinking to post this as comment to either your original question or the answer, but I chose to write an "answer" only because this is an honest question and I need some formatting capabilities.
I have multiple ASP.NET apps running on by site. In particular the root website is running an MVC4 app.
Since I cannot have WordPress installed at the root, my plan was to have it on its own app folder http://mydomain.com/wordpress/ and then have a URL-rewrite rule that to do the following (using peudo-code):
blog.mydomain.com/{path} --> mydomain.com/wordpress/{path}
I've only caused a mess with this approach and have not been successful using pretty permalinks, sometimes getting into redirect-loops and other times breaking links to .css files, admin pages, etc...
Have you ever given this a thought, i.e., having wordpress as a subapp instead and do sub-domain URL-rewriting?!?!
I had a similar situation but I had no need to edit my web.config file. Instead I followed instructions here at https://wordpress.org/support/article/giving-wordpress-its-own-directory/ where this is documented.
At point 7) within Moving WordPress process to a subfolder Method II (With URL change) you find options for a IIS installation.
I'm developing a webapplication where users will have a custom url just like in Twitter (twitter.com/holiveira). I've created a redirection rule that points to a page where I use the string after the domain name to search in the database.
The problem is that this rule is preventing the Scriptresourse.axd files used by Asp.Net Ajax Client Framework from loading properly.
Any idea how to solve this issue?
This is the rule I'm using:
<rule name="RewriteDropbox" 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="dropbox.aspx?clienturl={R:1}"/>
</rule>
I figured out the problem. The rewrite rule was to loose, since I was not adding the domain to it, and this caused the requests fo the ScriptResource.axd file to get lost, because it was redirected to my dropbox page.
When I added the domain to the rule it worked perfectly.