I have a JSP page which is named as erSelection.jsp. But, at the deployment in the configuration file it is stored as ERSelection.jsp as a result page not found error is found. Is there a way in JSF we can redirect the URL http://localhost:8080/Test/ERSelection.faces to erSelection.faces.
If just renaming the physical file is really not an option, your best bet is to either create a custom filter which does that
if (request.getRequestURI().endsWith("/ERSelection.faces")) {
response.sendRedirect(request.getContextPath() + "/erSelection.faces");
// You may want to do a 301 instead with a Location header.
} else {
chain.doFilter(request, response);
}
or to grab a more customizeable URL rewrite filter which does that by a simple configuration file, such as the Tuckey's URLRewriteFilter.
Related
I'm trying to get Caddy server to display a file that is kind of dynamic (assigned to a variable). I find the official documentation a bit opaque and searching Internets doesn't return much info either.
My example Caddyfile config:
example.com {
route / {
# own plugin that adds custom headers to the request
my_plugin do_something
map {header.something} {my_file} {
x "x.html"
y "y.html"
default "404.html"
}
file_server {
browse {my_file}
}
}
}
So the idea is Caddy displays "y.html" template if the request contains "something" with value "y". You get the picture.
However Caddy will complain with:
http.log.error parsing browse template: parsing browse template file: open {my_file}: no such file or directory
Looks like it takes the "{my_file}" in literal matters(!).
How does one gets this done in Caddy?
N.B
Other examples such as redir {my_file} work and redirect to "example.com/y.html".
Or is there better way in general to display a template "from a variable" ?
templates {my_file} does not work.
Okay, I figured it out. Eventually.
Simple yet not-so-obvious syntax:
*.example.com {
map {labels.2} {my_file} {
x "x.html"
default "404.html"
}
root * tmpl
file_server
templates
try_files {my_file}
}
So if the domain is x.example.com it will then render the x.html template located in current working directory's tmpl folder. The templates directive is only needed if the template(s) contain any functions/actions (e.g. include).
I am trying to save the HTTP_REFERER to a $_SESSION[] variable and only updating it if the referring page does not match the current page. This way if I reload the page through a PHP script it does not save the current page I'm on as the referrer.
First I am getting the REFERER and comparing it to the current URL:
$referer = $_SERVER["HTTP_REFERER"];
preg_match('/^(.*?\:\/\/)/', $referer, $start);
$url = $start[0].$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
Then I have an IF statement that says if they DONT match to update Session:
if($url != $referer){
$_SESSION['referer'] = $_SERVER["HTTP_REFERER"];
}
Then I check the output:
echo $_SESSION['referer'];
The issue is that it updates the session no matter what!
I have tried to do the same with cookies instead of the session variable. With cookies, I can see it changing in the consol. It won't update, and then a second later it updates. Almost as if the page is loading twice.
When I set this code
setcookie('test', $_SERVER["HTTP_REFERER"]);
When I go to a new page, it will correctly display the correct refer, and then after a second, it switches to the current page.
My current setup uses .htaccess so that I don't have to type the file names of the pages. I have all requests of any subfolders go to /index.php
So http://example.com/home or http://example.com/coolpage will all go back to http://example.com/index.php
Then with PHP I look for the file named the same as that subfolder and then "include" it in my index.php file as a sort of simple templating system.
Are these .htaccess redirects messing with the $_SERVER["HTTP_REFERER"]? If I just use the $_SERVER["HTTP_REFERER"] directly within my code it works fine (it's not updating). It's only when I try to save it to another variable that things go wonky.
Is there a better way to have a back button that sends the user back to where they came from without taking into account multiple page loads of the same URI?
I found a workaround that seems a bit odd, but it achieves the effect I want.
I wrap the whole thing in an if statement only executing if the $uri != favicon.ico
if($uri != 'favicon.ico'){
$referer = $_SERVER["HTTP_REFERER"];
preg_match('/^(.*?\:\/\/)/', $referer, $start);
$url = $start[0].$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if($url != $referer){
$_SESSION['referer'] = $_SERVER["HTTP_REFERER"];
}
$backButton = $_SESSION['referer'];
}
I have a web api 2 project, and in my code I do some routing stuff myself.
I have all my actions going to a single route, so hitting localhost/<anything> will always go to one route.
In that route I am doing some custom pattern matching.
If the user goes to /Kittens/AdoptAKitten/12345
It will match against a template I have using regex, defined as /Kittens/AdoptAKitten/{something}
The problem is when I host my project locally, it ends up at localhost/KITTENCORP.ADOPTION/ which is the name of my project. As a result the route matching doesn't work.
I am not sure how to take into account this 'root' address. Previously I was just looking at the domain part of the Uri object but I need to include this part in the comparison to make it work (or disregard/remove it).
This code will however also be deployed to a server somewhere at which point it will probably be hosted on adoptionservice.kittens.org and thus adoptionservice.kittens.org/Kittens/AdoptAKitten/12345 will be the url. So it has to account for both situations.
Any ideas how I can resolve this problem?
For anyone stumbling across this and wondering the same thing, I fixed it with this code:
// When hosted in IIS it may get a virtual path such as localhost/KittenLibrary that needs including in comparisons
if (!string.IsNullOrEmpty(HttpRuntime.AppDomainAppVirtualPath))
{
urlStart = UrlCombine(urlStart, HttpRuntime.AppDomainAppVirtualPath);
}
UrlCombine is a function I pinched from another SO question:
private static string UrlCombine(string url1, string url2)
{
if (url1.Length == 0)
{
return url2;
}
if (url2.Length == 0)
{
return url1;
}
url1 = url1.TrimEnd('/', '\\');
url2 = url2.TrimStart('/', '\\');
return string.Format("{0}/{1}", url1, url2);
}
I am performing a simple AJAX() request using Jquery (Google hosted 1.7.1 jquery.min.js code)
The code is pretty simple:
$.ajax({
type: "POST",
url: "../inc/ajax_msgread_sendPM.php",
data: "fromuserid=<?php echo $fromuserid; ?>&pmSubject=<?php echo urlencode($pmSubject); ?>&pmBody=" + pmReply,
success: function(data){
$("#showSuccess").show("fast");
$("#resultResponse").html(data);
}
});
The mod_rewrite .htaccess for this document is:
RewriteRule ^messages/read/([^/]+)/([^/]+)/?$ /msgread.php?usernam=$1&keynode=$2 [QSA,L]
When I view the $resultResponse for some reason the Ajax keeps wanting to turn $1 into 'inc' so any unrelated (or related) mySQL queries using $_GET["usernam"] from the URL ends up returning 'inc'
Why is this happening? There is no relation between my script and the mod_rewrite. There are no variables named "usernam" or "1" on the script (or anywhere on the site).
Firebug gives no help.
Advice please?
UPDATE:
I see where the problem is coming from.. in the ajax jquery code:
url: "../inc/ajax_msgread_sendPM.php",
the "inc" keeps getting set as the username because of its location based on the mod_rewrite rules... I need to somehow exclude this from mod_rewrite... just not sure how to solve this problem
As long as current url for your page is
www.domain.com/messages/read/username/NQ
and you use relative path - it is being rewritten to
www.domain.com/messages/read/username/inc/ajax_msgread_sendPM.php
which is definitely not what you want. The simplest solution would be to change the ajax endpoint url to:
url: "/inc/ajax_msgread_sendPM.php",
I haven't found all the answer to my current problem.
Here is the root of the site:
cache
img
display.php
admin.php
What I need is to block all the direct access of the files and allow only access via url formatted like that:
1 ht*p://sub.domain.com/image/param/size/folder/img.jpg (param, size, folder, img are parameters)
2 ht*p://sub.domain.com/action/param1/param2/ (param1, param2 are parameters)
1 would point to display.php with the correct parameters
2 would point to admin.php with the correct parameters
Every other access must be 404 (at best) or 403
my rules are (the htaccess is in ht*p://sub.domain.com/):
RewriteRule ^image/([^/]+)/([0-9]+)/([^/]+)/([^/]+)\.jpg display.php?param=$1&size=$2&folder=$3&img=$4 [L]
RewriteRule ^action/([^/]+)/([^/]+) admin.php?action=$1¶m=$2 [L]
Those rules work as I want to but I am stuck on how to block any access that does not come from those URL!
Also (as a bonus) I would like to be able to use the same htaccess on diferrent web address without having to change this file.
Thanks in advance
Have you try moving the image out of the public folder and use php to call the image in?
For the PHP files you can use the switch statement (http://www.php.net/switch).
For the admin.php file you can do something like:
$get_action = $_GET['action'];
switch ($get_action) {
case "edit":
case "view":
case "delete":
case "add":
//Continue loading the page
break;
default:
header('HTTP/1.1 403 Forbidden');
die();
}
Note: I don't know how your code looks or works, but you can have an idea base on the code I added.