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.
Related
I want to redirect "abc.aspx/" to "abc.aspx". How can this be done?
Page gets broken when requested page ends with '/'. How to handle such type of requests?
Is there any rewriting rule that can be added into web.config file?
In your web.config, under the system.webServer, add
<rewrite>
<rules>
<!--To always remove trailing slash from the URL-->
<rule name="Remove trailing slash" 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>
</rules>
</rewrite>
Some Gotchas
In your development environment, if you run your web site under Visual Studio Development Sever, you won't be able to see this feature working. You will need to configure your application to run under at least IIS Express.
When you deploy your web site and see that this feature is not working on your production server, it will be because you miss configured something. One of the common mistakes is having overrideModeDefault attribute set to Deny for rules under <sectionGroup name="rewrite"> inside your applicationHost.config file.
If you are on a shared hosting environment and you see that this feature is not working, ask your provider if they have given you the permission of configuring this part.
Source: http://www.tugberkugurlu.com/archive/remove-trailing-slash-from-the-urls-of-your-asp-net-web-site-with-iis-7-url-rewrite-module
I know this is an old post, but Mihai's answer can make your application vulnerable to open redirects attack (see this)
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
This redirect URL can be used for an external redirect, so make sure you have other validations in place before this rule is evaluated or change the redirect to make it internal instead. If you really want to remove the trailing slash and keep it internal, you can change it to:
<action type="Redirect" redirectType="Permanent" url="/{R:1}" />
Cheers
My company launched a redesigned version of our site this past October. We have a normal ASP.NET domain setup, but some pages were renamed and moved to new locations, so we had to rely on the URL Rewrite module in Server 2012 to ensure that bookmarks were routing to the correct locations.
In most cases, a 1:1 URL match based on regex patterns or a rewrite map works fine and as you would expect. However, we have some legacy directories that I had to move over as well, that weren't a part of the redesign project. I moved these folders into a folder off of the root, something like:
$/sandbox/annualreport/
so that we can monitor and update all of the folders that haven't been redesgined in one place. The abridged rewrite entry I am using is this:
<rewrite>
<rules>
<clear />
<rule name="Sandbox URLs">
<match url="^(annualreport)\b/?(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="sandbox/{R:1}/{R:2}" />
</rule>
</rules>
</rewrite>
This works fine for all URLs when the directories are followed by a trailing slash, like $/sandbox/annualreport/ but not when the trailing slash is left off, like $/sandbox/annualreport, even though the / is optional. The pages load and they are accessed correctly, but the problem is, some of these directories have local css, images, and scripts, which are accessed in the HTML like href="css/main.css" or src="scripts.js". When I view source and click on those style or js links, the rewrite looks for these files off of the root, unless the trailing slash is added.
This has become a nuisance because I have to have a 301 redirect for every "sandbox URL" (before this rewrite entry) to add the trailing slash. This works, but I have to specify every folder name in the regex:
<rule name="Custom Add Trailing Slash" stopProcessing="false">
<match url="^(annualreport)\b$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:0}/" />
</rule>
I have tried the default URL Rewrite wizard for universally adding a trailing slash, which I would prefer, but it doesn't seem to fix the localized files problem:
<rule name="AddTrailingSlashRule1" stopProcessing="false">
<match url="(.*[^/])$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" />
</rule>
How can I force a trailing slash to be added on a URL rewrite while maintaining the locally accessed files within the rewrite directories?
Any help would be greatly appreciated. Thanks.
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/
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.