Redirect http to https via IIS - iis

I have this application that I just installed a SSL certificate for. Yest I tried to redirect the users to use only the HTTPS://url.com. and prevent them from using the http://url.com site. However because I lack understanding the regular expressions to define the Pattern and the condition and unfortunately, I could not find a guide with some example of how to define those rules. I would like a concrete example of how to set this up https://{HTTP_HOST}{REQUEST_URI}.

Ensure you have the URL Rewrite feature added. In IIS manager configure the following in the URL rewrite section.
Create inbound rule (Blank Rule)
Requested URL : Matches the pattern
Using: Wildcards
Pattern: *
Conditions
Input: {HTTPS}
Type: Matches the pattern
Pattern: off
Action
Action Type: Redirect
Redirect URL: https://{HTTP_HOST}{REQUEST_URI}
Append Query String Checked
Redirect type: Found (302)
Once you have done this. Create a condition...
Right click your new rule
Conditions -> Add+
Condition Input : {QUERY_STRING}
Matches the Pattern
Pattern: off
Essentially it should look like this:
All traffic using a http request will be automatically redirected to the https port.

I think I've found a solution without url rewrite. In IIS, right click on the website, choose "Manage web site - Advanced settings", expand "Behaviour", expand "HSTS" and set to "True" the properties "Enabled" and "RedirectHttpToHttps".
Update:
As #jonasfh pointed out, you need anyway to have bindings both to http and https, because only the successful requests to http are redirected to https. So, if the request to http isn't successful (because http binding is not present) the redirect to https doesn't happen. Thank you.
Update2: just wanted to add that after the settings a site restart from IIS is needed.

My method from Global.asax
protected void Application_BeginRequest()
{
#if !DEBUG
if (!Context.Request.IsSecureConnection)
{
if (Context.Request.Url.ToString().Contains(".well-known")) return;
Response.StatusCode = 301;
Response.RedirectPermanent(Context.Request.Url.ToString().Replace("http:", "https:"));
}
#endif
}

Related

Redirect HTTP POST request to another sub-domain if some parameter is not empty

I am integrating my web app with Cloudflare and Cloudflare responds with 524 to long running processes after 100 seconds.
Cloudflare offers the solution to make a separate sub-domain for this process. Ok, I want to redirect my HTTP POST request to my newly created subdomain, if a specific parameter's value is not empty.
Running Linux with nginx as a web server. Following link only shows how to redirect from one sub-domain to another in Cloudflare but I want redirect on the basis of POST parameter's value.
[link] https://webmasters.stackexchange.com/questions/105897/cloudflare-dns-how-to-301-redirect-all-traffic-from-sub-domain-to-main-domain-w
For Example, Considering evidence parameter, the following is the data of POST request. As show below, this request should not be redirected to new/another subdomain.
{
"request_id" : "",
"callback_url" : "http://www.example.com/",
"email" : "",
"document" : {
"evidence" : "",
"document_number" : ""
}
}
But if evidence parameter is not empty as show below, then this request should be redirected to new/another subdomain.
{
"request_id" : "",
"callback_url" : "http://www.example.com/",
"email" : "",
"document" : {
"evidence" : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAALCAYAAABCm8wlAAAABmJLR0QA=",
"document_number" : ""
}
}
Is there any nginx configuration rule or Cloudflare Page rule that can solve this problem? I'll thankful to you.
That's not possible with the default nginx installation. You'll need to install lua-nginx-module and use ngx.req.get_body_data. I think the documentation is clear on how to to what you asked.
https://github.com/openresty/lua-nginx-module
https://github.com/openresty/lua-nginx-module#ngxreqget_body_data
A few points to remember:
Use a temporary redirect, not a permanent redirect, because it's dependant upon the content posted, and you don't want to break any bookmarks for the page.
Use a 307 not a 301. A 307 should redirect a POST, whereas a 301 should redirect to a GET in a modern browser.
The user might be asked if they want to resubmit, so your site might end up black listed for appearing to phish.
Another option is to use a Cloudflare worker to handle the POST request. The worker could forward the POST request to the correct back end, without affecting the user's experience at all.

Netlify pretty query

I want to host a Netlify website where you can search for specific users. Currently its like this https://example.com/users?q=exampleuser . (its https://example.com/users.html but pretty url'ed)
But what I want is to make the URL query pretty. So the endresult should be https://example.com/users/exampleuser but it should still be a url query so the JavaScript can make calls based on the query.
e.g.:
https://example.com/users?q=test123 to
https://example.com/users/test123
https://example.com/users?q=example456 to
https://example.com/users/example456
The rewrite rule will work with:
/users q=:q /users/:q 200
When you navigate to https://example.com/users?q=exampleuser, you must have an existing endpoint at https://example.com/users/exampleuser/ or it will give a 404 status code, but will still rewrite the original path.
Note: If you have an existing endpoint at /users/ this method will not fallback if the rewrite path has an invalid endpoint. Meaning you can't fallback to /users/ endpoint if the query path is an invalid endpoint.

Redirection to incorrect page

I am trying to redirect using a button on a custom control (code below). the following is written to the debug toolbar which is where I want to go:
destBack=https://www.example.com/MyAttachments . But I instead get a Error 404 page and the following line appears on the server console:
HTTP Web Server: Item Not Found Exception [/site/home.nsf/https:/www.example.com/MyAttachments.xsp] Anonymous
I do have a reditection rule as follows:
Description: MyAttachmentsView
Type of rule: Redirection
Incoming URL pattern: */MyAttachments
Redirect to this URL: /site/home.nsf/MyProfileAttachmentsView.xsp
Send 301 Redirect:
If I copy and paste the destBack URL I get where I want to go.
My SSJS code behind the button is as follows
importPackage(com.example);
var destination = configBean.getValue("HostURL")+"MyAttachments";
dBar.info("destBack="+destination)
context.redirectToPage(destination)
Try this code to redirect
externalCtx = facesContext.getExternalContext();
externalCtx.redirect("http://www.tlcc.com");
See http://linqed.eu/2011/07/27/xpages-server-vs-client-side-redirects/
context.redirectToPage is designed to redirect the XPages runtime to an XPage within the current database. That's why the URL in the error message contains "/site/home.nsf/" (the current database path) and "https:/www.example.com/MyAttachments.xsp" (the URL you're defining).
If you want to change the whole URL, you need to change the URL client-side, not server-side, e.g. with location.href="...."

Routes in Codeigniter

I want to have a clean URL in CodeIgniter based application for User's Profile Information.
Please Take a look at URL formats below.
Actual URL : http://www.mydomain.com/index.php/users/profile/user1
I'm expecting users to have Personal URL's like
http://www.mydomain.com/user1
http://www.mydomain.com/user2
http://www.mydomain.com/user3
URL http://www.mydomain.com/user1 should process http://www.mydomain.com/index.php/users/profile/user1 in background execution.
I will be removing index.php from URL using Route library.
Thanks in advance for any sort of help.
Have a look at https://www.codeigniter.com/user_guide/general/routing.html.
$route['user(:num)'] = "users/profile/user/$1";
If you mean you want /anyusername to route to the users controller, you would have to put:
$route['(:any)'] = "users/profile/$1";
At the bottom of routes.php and every non user-URL above it. Otherwise every URL would be routed there, obviously. You will need to implement some mechanism in the users-controller to throw 404-errors, since you are routing all requests not catched in the routing rules above.
IN config/routes.php
add this line
$route['user(:num)'] = "users/profile/$1";

setting redirect page for my Joomla website hosted in IIS

I am using Joomla with IIS in my website, and now i am trying to set a 404 redirect page in my Joomla website, i could use htaccess to make this when i am using apache, but for this IIS its gets tricky it seems.
you can see my web.config file here
any suggestion would be helpful..
There is no module available AFAIK - at least not for 1.6. But it is quite easy to do by yourself. You can use the solution in the joomla documentation. This is usable for any error code.
http://docs.joomla.org/Creating_a_Custom_404_Error_Page
if (($this->error->code) == '404') {
header('Location: /search');
exit;
}
The part behind the Location: is where you redirect to the page you want. e.g. /search in this case. The above is for 1.5, for 1.6 you need to use this:
if ($this->error->getCode() == '404') {
header('Location: /search');
exit;
} ;
in your IIS Console, right click on the website, goto the Custom Errors TAB,
scroll down to HTTP Error 404, Edit Properties, change the Message Type
to URL, and then type in the URL you want to change to.

Resources