I have a request from a client that they would like for me to have this happen. When a user on the web goes to http://x.x.x.x have it 301 redirect to their domain name http://www.domain.com . I have messed around with rules in URL Rewrite and I have no problem specifying the conditions to determine the request for the IP address, etc. But when I do the redirect, the site goes into a redirect loop.
My question is how can I do this ? I know this is basically redirecting to the same place, hence the loop, but is there any way to make this happen ? For SEO reasons, they don't want visitors to the site to be able to get it via the public IP.
Thanks,
Chris
//////
Ok All, Thanks to some direction and good ideas from Caner, I was able to figure it out.
Here is the ASP code I used:
<%
if Request.ServerVariables("HTTP_X_HOST") = "" then
Dim rd
rd = "http://www.xxx.com" & Request.ServerVariables("PATH_INFO")
Response.Status="301 Moved Permanently"
Response.AddHeader "Location",rd
Response.end
end if
%>
Thanks to all the suggestions!
Chris
It is a kind of tricky, but instead of configuring this on IIS, I suggest your client to add some code in their header file( header.asp file, site.master etc..);
This code can check the request; and you can reach HttpContext.Current.Request.Url.Host.
Using HttpContext.Current.Request.Url.Host, you can do a redirect.
string currentURL = HttpContext.Current.Request.Url.Host;
if(!currentURL.StartsWith("www."))
{
string newURL = HttpContext.Current.Request.Url.Host;
if(currentURL =="www.x.x.x.x")
newURL ="www.domain.com";
newURL += HttpContext.Current.Request.Url.PathAndQuery;
Response.Redirect(newURL);
}
Hope this helps.
Related
assume the following:
Your model has: Products.slug
urls: path('<int:id>/<slug:slug>/', views.product_detail, name='product_detail'),
views: products = Products.objects.get(id=id, slug=slug)
Someone goes to /products/1/brazil-nuts/, and it goes to the right page.
But then someone copy/pastes the url wrong to: /products/1/brazil-nu/
Then you get a DoesNotExist error... Now you could "fix" this by not forcing the slug=slug argument in your query, but then the url is incorrect, and people could link from /products/1/brazil-nu-sjfhkjfg-dfh, which is sloppy.
So how would I redirect an incorrect url to the correct one with the same url structure as the correct one (only id and slug arguments), without relying on JavaScript's window.history.pushState(null, null, proper_url); every time the page loads?
I've already tried making the same url pattern to redirect, but since django uses the first pattern match, it won't work no matter where you put the url in your list.
Just update your view:
products_q = Products.objects.filter(id=id, slug__startswith=slug)
products_count = products_q.count()
if products_count == 1:
product = products_q.first()
return reverse_lazy('product_detail', args=[product.id, product.slug])
By using jQuery I have sent a POST value to another file using window.location
where bname refers to some name from database
$.form(window.location + "?rt=" + bname,{project:project,bname:bname},'POST').submit();
after jQuery execution the following url appears on screen: /m/?rt=Jaypee%20Group
However I need the above one as /m/JaypeeGroup
I have tried htaccess rewrite but can't fix this issue
Is there any other way ("window.location + "?rt=" + bname") in jQuery or how can I fix this in htaccess?
Don't need for htaccess, just replace the space in javascript :
$.form(window.location + "?rt=" + bname.replace(" ", ""),{project:project,bname:bname},'POST').submit();
Invoke JQuery as follows.
$.form(window.hostname+"/"+context+"/m/"+project+"/"+bname+"/","POST").submit();
and add htaccess rewrite as per your requirements.
yourhostname/context/m/(.*)/(.*)/ .......
I'm using the wai-app-static static package to serve a small website. Originally I called it as:
staticApp defaultFileServerSettings root
and all was right with the world. I wanted to switch to using defaultWebAppSettings, though, (as this is a website). I was able to get that to work, such that if I went to http://localhost:3000/index.html, it was fine, but I also need to set up a redirect from the root folder to the index site (viz. http://localhost:3000 --> http://localhost:3000/index.html).
Based on what I saw in the code, I tried a couple of variations of:
(defaultWebAppSettings root) { ssIndices = map unsafeToPiece ["index.html"],
ssRedirectToIndex = True }
I'm able to compile and run the server, but I can't get the redirection to work.
Any pointers or ideas would be greatly appreciated!
Thanks
Hmm, I didn't know about the ssIndices or ssRedirectToIndex settings. What I did for my app that has dynamic and static parts, was on requests to /, alter that request to say /index.html instead and send it to the "static server".
https://github.com/LeifW/online-typechecker/blob/master/index.hs#L20
This code works for me
httpApp :: Wai.Application
httpApp = Static.staticApp $ s {Static.ssIndices = [Static.unsafeToPiece "index.html"]} where s = Static.defaultWebAppSettings "./static"
we recently have put Varnish in front of our Drupal because the server was suffering heavy load and we are very pleased in general.
The only problem remaining is that we sometimes have an infinite redirection loop in the cached data. We have found this through our HTTP-Monitoring. We check the front page every minute. The page in the cache sometimes contains the full front page, but with a Location header set, that sends the user to the front page again.
We are not quite sure what could cause this, but also have no clue on how could track this down. Of course, the best way to handle this would be on the drupal side, but we can't really tell why this does happen.
Is there a way to log the cases when this happens? Or is it possible to detect this in varnish and mark the current cache content as invalid?
Of course, we don't want to always pass intentional redirects to the origin server, but the ones that would cause an infinite loop.
I hope to hear some ideas how we can further track this down. Many thank in advance for all kinds of hints.
I have found a workaround for this:
sub vcl_fetch {
// Fix a strange problem: HTTP 301 redirects to the same page sometimes go in$
if (beresp.http.Location == "http://" + req.http.host + req.url) {
if (req.restarts > 2) {
unset beresp.http.Location;
#set beresp.http.X-Restarts = req.restarts;
} else {
return (restart);
}
}
}
I give the backend a second (and thirhd) chance to return a proper page. If that fails as well, the Location header is removed. This works, because the proper page is served with just an additional invalid Location header.
The accepted answer by #philip updated for Varnish 4:
sub vcl_backend_response {
#Fix a strange problem: HTTP 301 redirects to the same page sometimes go in$
if (beresp.http.Location == "http://" + bereq.http.host + bereq.url) {
if (bereq.retries > 2) {
unset beresp.http.Location;
#set beresp.http.X-Restarts = bereq.retries;
} else {
return (retry);
}
}
}
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.