URL Rewriter Managed Fusion strip a QueryString - url

I'm Using Managed Fusion URL Rewriter, so far it has been awesome.
Now I just need to make sure a query string is passed in a URL from a proxy website.
I Need to pass CODE=777 on every call so I have the code below:
RewriteRule ^(.*) http://www.somewebsite.com/$1?CODE=777[P, QSA,I,L]
However I need to strip the query string before hand. So I used the code below:
RewriteCond %{QUERY_STRING} ^CODE=([0-9]+)+$
RewriteRule ^(.*) http://www.somewebsite.com/$1? [P, QSA]
RewriteRule ^(.*) http://www.somewebsite.com/$1?CODE=777[P, QSA,I,L]
But it fails when I redirect with additional querystring such as below
http://proxyserver.com?othercode=something
Any Ideas how to strip a particular querystring without removing all query strings paramters?

I downloaded the source code from Managed Fusion and added the project to my solution.
I searched and found this bit of code below that appends the QueryString. I updated the code by adding a new method to remove the querystring CODE and affix a FIXED one.
I also added a Setting to control the FIXED Value assigned to the CODE.
private Uri AppendQueryString(Uri substituedUrl, Uri existingUrl)
{
string append = existingUrl.Query.TrimStart('?');
if (!String.IsNullOrEmpty(append))
{
UriBuilder builder = new UriBuilder(substituedUrl);
if (String.IsNullOrEmpty(builder.Query) || builder.Query == "?")
builder.Query = append;
else
builder.Query = builder.Query.TrimStart('?') + "&" + append;
return AppendFixedQueryString(builder.Uri, existingUrl);
}
return AppendFixedQueryString(substituedUrl, existingUrl);
}
ADDED NEW METHOD
private Uri AppendFixedQueryString(Uri substituedUrl, Uri existingUrl)
{
string append = string.Format("CODE={0}", Settings.Default.CODE);
if (!String.IsNullOrEmpty(append))
{
UriBuilder builder = new UriBuilder(substituedUrl);
builder.Query = Regex.Replace(builder.Query, #"CODE=([0-9]+)", String.Empty);
if (String.IsNullOrEmpty(builder.Query) || builder.Query == "?")
builder.Query = append;
else
builder.Query = builder.Query.TrimStart('?').Trim("&".ToCharArray()) + "&" + append;
return builder.Uri;
}
return substituedUrl;
}
With this modification. even if the user explictly types CODE=123 at the URL it will just be ignored and a fixed CODE value will be assigned

Related

URL redirect from one domain to another using cloudflare

I haaave been trying to set up a cloudflare worker to redirecet an URL to another (SOURCE.ie/... -> DESTINATION.com/...).
The destination URL is set up within cloudflare the source-URL however is purchased via Onlydomains.
I have been using the code given in the documentation:
const base = "https://DESTINATION.com"
const statusCode = 301
async function handleRequest(request) {
const url = new URL(request.url)
const { pathname, search } = url
const destinationURL = base + pathname + search
return Response.redirect(destinationURL, statusCode)
}
addEventListener("fetch", async event => {
event.respondWith(handleRequest(event.request))
})
What I think have to do now is to aadd a route but is does not want to do that:
Although I added the nameservers to the Onlydomains URL.
Am I missing something? I just want a simple redirect that replaces SOURCE with DESTINATION :|

Legacy PHP urls do not invoke custom routing in MVC

We recently replaced an old php website with an asp.net MVC website. In order to prevent 404 errors from the legacy urls in search engines, we setup a custom legacy route system via - http://www.mikesdotnetting.com/Article/108/Handling-Legacy-URLs-with-ASP.NET-MVC
The code works on my local machine, and it redirects to the correct route; however, the live server issues a 404. Bonus problem/clue, the 404 is not our custom 404 page but the iis 6 default page.
The code:
public class LegacyUrlRoute: RouteBase
{
// source: http://www.mikesdotnetting.com/Article/108/Handling-Legacy-URLs-with-ASP.NET-MVC
public override RouteData GetRouteData(HttpContextBase httpContext)
{
const string status = "301 Moved Permanently";
var request = httpContext.Request;
var response = httpContext.Response;
var legacyUrl = request.Url.ToString();
var newUrl = "";
if (legacyUrl.Contains(".php"))
{
newUrl = "/";
if (legacyUrl.Contains("support/mailfilter.php"))
newUrl = "/support/";
else if (legacyUrl.Contains("/support/default.php"))
newUrl = "/support/";
else if (legacyUrl.Contains("/business/default.php"))
newUrl = "/services/";
else if (legacyUrl.Contains("/residential/default.php"))
newUrl = "/services/";
else if (legacyUrl.Contains("/about/default.php"))
newUrl = "/home/about/";
else if (legacyUrl.Contains("/jobs.php"))
newUrl = "/jobs/";
else if (legacyUrl.Contains("/support/links.php"))
newUrl = "/support/";
else if (legacyUrl.Contains("/support/settings.php"))
newUrl = "/support/";
else if (legacyUrl.Contains("/default.php"))
newUrl = "/";
response.Status = status;
response.RedirectLocation = newUrl;
response.End();
}
return null;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
return null;
}
}
Note:
We suspect that the problem is with iis being unable to serve php pages, but I can't seem to find a setting in iis to fix the problem. It is as if the request never hits the Controller code, error or otherwise. All other pages/routing is working perfectly.
Found the answer.
The problem is due to the fact that iis never starts the asp.net service if the extension is .php.
The solution is to go to Properties>Home Directory>Configuration, find the .php extension, change the executable path to the asp.net path, and limit it to "GET,HEAD,POST,DEBUG" or whatever you prefer. I originally selected the "All Verbs" radio button, but that did not work.
Though the page did not specifically have the answer I came up with, this page did help.

get absolute url on handle ressource request on blackberry

I use this method the get the urls of ressources contain on web page
public InputConnection handleResourceRequest(BrowserFieldRequest request) throws Exception
{
final String url = request.getURL();
return super.handleResourceRequest(request);
}
But, I made request.getURL(); it returns relative url and not the absolute url.
How can I change it to get the absolute URL?
When I run your code, it does return me absolute URLs, even when my web page contained relative links. That said, it wouldn't surprise me if sometimes, it doesn't. I haven't fully tested this code, but I would think you could try something like this.
Basically, you check to see if the URL is absolute, and if not, you assemble an absolute URL by using the parent BrowserField document URL:
ProtocolController controller = new ProtocolController(_browserField) {
public InputConnection handleResourceRequest(BrowserFieldRequest request) throws Exception {
String absoluteUrl = null;
URI uri = URI.create(request.getURL());
if (uri.isAbsolute()) {
absoluteUrl = request.getURL();
} else {
String docUrl = _browserField.getDocumentUrl();
String url = request.getURL();
if (url.startsWith("/")) {
// the URL is relative to the server root
URI docUri = URI.create(docUrl);
absoluteUrl = docUri.getScheme() + "://" + docUri.getHost() + url;
} else {
// the URL is relative to the document URL
absoluteUrl = docUrl + url;
}
}
System.out.println(" requesting: " + absoluteUrl);
return super.handleResourceRequest(request);
}
}
Again, for me, I was getting absolute URLs, so I couldn't easily test the code in the branch where the URL is relative. So, it's possible that I'm dropping a "/" somewhere, or not handling file:/// URLs properly.
But, this is a starting point, to workaround your problem.

How do I serve static files from a different URL with Dart?

With Dart, I've got awesome.html, but I'd like it to be /awesome. Is this purely an .htaccess (I'm using Apache) thing, or is there a way to go about this the Dart or "modern web development" way?
This .htaccess bit directs /awesome to /awesome.html:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
But then all my relative URL references (to css/js/images) break, and if I rewrite them from "assets/whatever" to "/assets/whatever" it'll break when working in the Dart Editor since it uses URLs like:
http://127.0.0.1:3030/Users/dave/Sites/my-dart-app/web/awesome.html
Ideas? Best practices? Thank you!
thanks for the question!
The answer depends on if you have a proxy or web server in front of your Dart server VM. If you have a proxy in front, then the proxy can do the URL rewriting before the request hits your Dart VM. This is a nice scenario anyway, because a proxy can do caching, SSL, load balancing, and more. The Dart VM is then just an "app server" in this scenario. I would recommend placing an industrial strength web server or proxy in front just as a best practice.
However, if you want to do URL masking and rewriting purely in Dart, here is some code. As Kai says in the comments above, this is generally a framework's job. But I'll include some code here anyway for fun. :)
import 'dart:io';
import 'dart:json';
class StaticFileHandler {
final String basePath;
StaticFileHandler(this.basePath);
_send404(HttpResponse response) {
response.statusCode = HttpStatus.NOT_FOUND;
response.outputStream.close();
}
String rewritePath(String path) {
String newPath = path;
if (path == '/' || path.endsWith('/')) {
newPath = '${path}index.html';
} else if (!path.endsWith('.html')) {
newPath = "${path}.html";
}
return newPath;
}
// TODO: etags, last-modified-since support
onRequest(HttpRequest request, HttpResponse response) {
String path = rewritePath(request.path);
final File file = new File('${basePath}${path}');
file.exists().then((found) {
if (found) {
file.fullPath().then((String fullPath) {
if (!fullPath.startsWith(basePath)) {
_send404(response);
} else {
file.openInputStream().pipe(response.outputStream);
}
});
} else {
_send404(response);
}
});
}
}
runServer(String basePath, int port) {
HttpServer server = new HttpServer();
server.defaultRequestHandler = new StaticFileHandler(basePath).onRequest;
server.onError = (error) => print(error);
server.listen('127.0.0.1', 1337);
print('listening for connections on $port');
}
main() {
var script = new File(new Options().script);
var directory = script.directorySync();
runServer("${directory.path}", 1337);
}
By the way, I've updated the rewritePath() function in Seth's code some so that it doesn't rewrite assets like .dart and .css files to .html, and so that it works w/ my client-side stuff living in /web.
String rewritePath(String path) {
String newPath = path;
if (path == '/' || path.endsWith('/')) {
newPath = '/web${path}index.html';
} else if (!path.endsWith('.html')) {
if (path.contains('.')) {
newPath = '/web${path}';
} else {
newPath = '/web${path}.html';
}
} else {
newPath = '/web${path}.html';
}
//peek into how it's rewriting the paths
print('$path -> $newPath');
return newPath;
}
It's of course super basic, and a framework that handles routing would certainly come in handy (would love to see what you're building #Kai).

How to change the default values of start and count parameters when picking the companies followed by a user in linkedin?

I am making a web-application in ASP.NET. I have used oauth to get profile fields of a user. I need the names of the companies followed by the user, but the problem is that the default value is set to 20. so, if the user is following more than 20 companies i am not able to get it. Please tell me how can i modify the start and count values. Iv used this url to make the call http://api.linkedin.com/v1/people/~:(following:(people,companies,industries,news-sources),educations).. Please help asap..
var requestHeader = GetUserProfileAuthorizationHeader();
var queryString = CreateQueryString();
var request = WebRequest.Create(RequestProfileUrl + queryString);
request.Headers.Add("Authorization", requestHeader.ToString());
request.Method = HttpMethod.Get;
try
{
var response = request.GetResponse();
using (var responseStream = response.GetResponseStream())
{
var reader = new StreamReader(responseStream);
var responseText = reader.ReadToEnd();
reader.Close();
return responseText;
}
}
Here public static string RequestProfileUrl = "http://api.linkedin.com/v1/people/~:(following:(companies:(id,name,size,industry),industries))"; And the method CreateQueryString() does this queryString = "?format=xml"; whenever i try to add something like queryString = "?format=xml&start=0&count=40"; it gives error dispite the number of companies followed being more than 60.. maybe i need to pass the query parameters in between the RequestProfileUrl i.e near the company somehow..
Did you tried adding ?start=x&count=y to the url?
Probably if you're getting an error when you add query parameters to the URL, you're not adding those parameters in the way that your OAuth library expects them to be added. You need to figure out how to add the parameters so they're added to the signature generation process or your signature will be invalid and you'll get a 401 error back from the server.

Resources