IIS does not run all URL rewrite rules, bails with 404 - iis

Okay, so I'm trying to do this, but in IIS: Apache mod_rewrite is removing slashes and generally ignoring my rules
I've successfully imported the rules, and they each work correctly on their own, but for some reason IIS is not running the full list of rules to completion, regardless of whether I have "stop processing" checked or unchecked for each rule. Instead it fails with a 404 after the first matched rule instead of continuing to match subsequent rules, which I believe would result in the correct file found.
Is this some sort of IIS debug setting? Can I do anything about it? Thanks!

Use Failed Request Tracing to trace/debug IIS Rewrite rules execution.
http://www.iis.net/learn/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules

Related

Having trouble redirecting URL when query string is present in .htaccess

I updated my website, and I'm attempting to make sure a bunch of old URLs redirect to the correct page. Some of my links are working fine, while others aren't.
Example:
Redirect 301 /compare-suites/ /en/our-products/compare-suites
Redirect 301 /compare-suites/?lang=fr /fr/nos-produits/comparaison-des-suites/
The first redirect works perfectly, but the second one redirects to /en/our-products/compare-suites?lang=fr, ignoring my second rule.
The alias module which offers the Redirect directive is unable to handle the query string inside a URL. That is clearly stated in the documentation. Along with the hint to use the rewriting module instead.
I strongly recommend that you start reading the documentation of the utilities you use:
Alias module: https://httpd.apache.org/docs/current/mod/mod_alias.html
Rewrite Module: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
As typical for OpenSource they are of excellent quality and come with great examples.
This would be the approach you are probably looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)lang=fr(&|$)
RewriteRule ^/?compare-suites/?$ /fr/nos-produits/comparaison-des-suites/ [QSD,R=301,END]
RewriteRule ^/?compare-suites/?$ /en/our-products/compare-suites [R=301,END]
Note that the more specialized exception has to be implemented before the more general fallback, so further up in the configuration file.
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

how to ignore path of URL with .htaccess

What is tried to accomplish without success is ignoring by .htaccess any path of a domain and just transfer it to the index page, for example
the url example.com/asb , the asb will be ignored and the page that will appear will be the main index page , (while the URL still be asb if possible)
how can I do it?
Simplest and recommended way to achieve this is by using FallbackResource
FallbackResource /index.php
As per official doc:
In earlier versions of httpd, this effect typically required mod_rewrite, and the use of the -f and -d tests for file and directory existence. This now requires only one line of configuration.
Existing files, such as images, css files, and so on, will be served normally.
This probably is the easiest approach:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^ /index.php [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
If the above does not show any effect, then most likely one of these issues needs to get solved:
rewriting module is not loaded into the apache http server
rewriting module is not enabled
rewriting is not permitted to the location you want it to get applied
you have implemented your rewriting rule at the wrong place
your http server is unable to read the rewriting rule
you have a syntax error in your rule set
All of these need to be tackled separately. You should start by monitoring your http server's error log file. What does it say?

Redirect urls with trailing slash

My website currently online is completely static and all the URLs have a trailing slash at the end : https://www.website.com/blog/article-1/
I'm working on my new website which is using Prestashop. On Prestashop, URLs don't have a trailing slash : https://www.website.com/blog/article-1
Problem: I have an excellent SEO on my current website and I need to keep the actual URLs (with trailing slash) available. For user experience, I'd like URLs to work with or without trailing slash.
How can I redirect my new URLs to the same URL + trailing slash? If possible, I'd like to rewrite URLs so that users always see the URL with a trailing slash.
Example :
https://www.website.com/blog/article-1/ is redirected to https://www.website.com/blog/article-1 and the URL visible in the address bar is https://www.website.com/blog/article-1/.
Well, ask "How can I redirect my new URLs to the same URL + trailing slash"...
The answer obviously is: by implementing exactly that rule. There are thousands of examples for this alone here on SO. None of those helped? Why not?
Anyway, here is another one:
RewriteEngine on
RewriteRule ^/blog/([^/]+)$ /blog/$1/ [R=301]
RewriteRule ^/blog/([^/]+)/$ /blog/$1 [END]
You need to take care to send out references with leading slashes with this setup. Since otherwise your site will be dead slow, since the clients will have to request every single page twice due to the redirection then required for every single page...
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
If you mean default prestashop links like products, categories etc. you can just change their way to be built. Prestsahop allows us to achieve this within admin-panel Configure->Shop Parameters->Traffic & SEO->SEO and URL's>Schema of URLs (for PS 1.7).
And there change an URL in interest, for example, Route to category
from {id}-{rewrite} to {id}-{rewrite}/. And you won't need to redirect anything.

Redirect rule creates loop

I have a redirect rule on htaccess that simply points a parent page to its child page.
Redirect 301 /swu-members-only/ /swu-members-only/home/
However, when I try to access the page i'm trying to redirect, it returns a URL like this: http://simonwu.com.au/swu-members-only/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home/home
What causes the redirect loop like this to happen? Any suggestions to fix is greatly appreciated.
The reason for the redirection loop is that the pattern you rewrite also matches your target path. So when the client sends the next request to the specified target it again gets redirected...
You can get around that by forcing an exact match:
RedirectMatch "^/swu-members-only/?$" "/swu-members-only/home/"
Another option is to use the rewriting module which offers much more flexibility:
RewriteEngine on
RewriteRule "^/?swu-members-only/?$" "/swu-members-only/home/" [R=302]
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug, they really slow down the http server, often for nothing and they can open pathways to security nightmares. They are only provided for situations where you do not have access to the real http servers host configuration (read: really cheap service providers).

Why does IIS 7.5 adds a trailing slash on folders? Can we disable courtesy redirect for a URL Rewrite rule that removes trailing slash?

IIS does URL cleanup on directories by adding a trailing slash. See this old docs from IIS 6:
IIS generates courtesy redirect when folder without trailing slash is requested
Why? Is the intent still relevant?
Any security implications?
How can I disable it to make this work with a URL Rewrite rule "RemoveTrailingSlashRule"
When you add a rule under IIS 7.5 with URL Rewrite 2, the rule will not be applied to directories (using IsDirectory) and folders (using IsFolder).
See this warning on Add a rule to append or remove the trailing slash symbol:
This will create the RemoveTrailingSlashRule1:
I have an answer for the specific case of a child IIS Application here: https://stackoverflow.com/a/25817317/292060. The child app seems to be the usual culprit, but isn't explicitly described in this question.
To try to answer the questions, here are my opinions from dealing with IIS and Microsoft for years. I don't have hard sources to cite; some of this is just gut feelings.
Why? Is the intent still relevant?
I think it stemmed from the original "default document" feature, namely index.html. Websites wanted their home page to just be the domain, then this extended to subfolders. With url rewriting, the intent isn't relevant anymore - you can rewrite to your heart's content, and would rather IIS get out of the way. It's common to want friendly urls, and no trailing slash (except for the domain/website root - that is required to have a trailing slash, even if some browsers like Chrome get cute and hide it).
Any security implications?
I think the only security implication was the original directory browsing. If you forgot to do a default document, and directory browsing was left turned on, then people could browse your website files. As far as I know, directory browsing has been long disabled as the default setting.
With any requests, whether trailing slash or not, url rewriting or not, your server and code need to withstand bad requests. This is true for all situations, not just specific to the slashes. http://xkcd.com/327/
How can I disable it to make this work with a URL Rewrite rule "RemoveTrailingSlashRule"
I have an answer if the issue is the child application, here: https://stackoverflow.com/a/25817317/292060
The summary is, in IIS:
Disable the Default Document feature for the child application.
Using Url Rewrite, create a rule to rewrite (not redirect) an empty request to default.aspx
If this question is for a more general issue, including regular subfolders even if not a child app, consider removing the "Is Not a Directory" from the rule, and let this redirect even when it sees a directory. That may work, or may create an infinite redirect loop, I'm not sure.

Resources