Lighttpd - redirect a specific URL to another domain - webserver

I want to redirect a particular url to another domain..
For example, redirect www.site1.com to www.site2.com using lighttpd
Note: It should not redirect www.site1.com/abc to www.site2.com/abc (or) www.site2.com
I tried it. But it redirects all requests to site2.com
Thanks in advance :)

$HTTP["host"] == "www.site1.com" {
$HTTP["url"] =~ "^/$" {
url.redirect = ( "" => "http://www.site2.com/" )
url.redirect-code = 302
}
}

Related

redirect a subdirectory to another domain in lighttpd

I am trying to redirect a subdirectory to a different domain, ideally https. I have some redirect rule in place to forward to http to https and its working but I want one particular subdirectory to forward to another domain. And it should forward example.com/subdir/whatever to newdomain.com/whatever
$HTTP["host"] =~ "example.com" {
accesslog.filename = "/var/log/lighttpd/example.com/access.log"
url.redirect = ( "^/(.*)" => "https://example.com/$1" )
server.name = "example.com"
}
}
Have you tried reading the documentation? https://wiki.lighttpd.net/Docs_ModRedirect
url.redirect = (
"^/subdir/whatever/(.*)" => "https://newdomain.com/whatever/$1",
"^/(.*)" => "https://example.com/$1"
)

Lighttpd - url redirect or re write - gUP to IP

I would like to achieve the following in lighttpd
redirect the following:
http://192.168.0.5/test/ to go to http://192.168.0.9/test
and
http://123.124.125.156/test/ test to go to http://123.124.125.158/test/
both 192.168.0.5 and 123.124.125.156 are addresses of the server
I am going in circles with my current knowledge.
I will keep trying and report if i get it before a reply
Regards
Liam
OK found the solution
$HTTP["host"] == "123.124.125.156" {
url.redirect = (
"^/test/(.*)" => "http://123.124.125.158/test/$1",
)
}
$HTTP["host"] == "192.168.0.5" {
url.redirect = (
"^/test/(.*)" => "http://192.168.0.9/test/$1",
)
}
if you want to redirect everything the use this inside the redirect
"^/(.*)" => "http://xxx.xxx.xxx.xxx/$1"
I hope this help someone
Regards
OK found the solution
$HTTP["host"] == "123.124.125.156" {
url.redirect = (
"^/test/(.*)" => "http://123.124.125.158/test/$1",
)
}
$HTTP["host"] == "192.168.0.5" {
url.redirect = (
"^/test/(.*)" => "http://192.168.0.9/test/$1",
)
}
if you want to redirect everything the use this inside the redirect
"^/(.*)" => "http://xxx.xxx.xxx.xxx/$1"
I hope this help someone
Regards

How to point subdomain to specific file with GET parameters in lighttpd?

I'd like a lighttpd server to redirect/rewrite visitors to a specific page when they come through a subdomain. Here is the example.
Website up and running:
www.example.com
Target page of said website:
www.example.com/subpage
Subdomain that I'd like to have pointed at the target page:
www.sub.example.com
So when a visitor types in : www.sub.example.com I'd like the browser to display the content from www.example.com/subpage
Is this possible? If so can you please explain to me how exactly?
I've tried fiddling with the vhost.conf file but to no avail. Here's what I tried:
$HTTP["host"] =~ "sub\.example\.com" {
server.document-root = "/var/www/example.com/www/"
server.error-handler-404 = "/index.php"
index-file.names = ( "/subpage" )
}
Thanks for any advice in advance!
One way to do it is use the url.redirect feature, to redirect www.sub.example.com to www.example.com/subpage
$HTTP["host"] =~ "example.com" {
$HTTP["host"] =~ "www.sub.example.com" {
url.redirect = (
"(.*)" => "http://www.example.com/subpage"
)
}
else $HTTP["host"] =~ "www.example.com" {
server.document-root = "/var/www/example.com/www/"
server.error-handler-404 = "/index.php"
}
}

Redirect rule that keeps connection mode (HTTPS | HTTP) in Lighttpd

I have a redirect rule so every request will be forced to have www. before them, the problem is that if someone connects through https:// they are redirect to http:// because I don't know what command should I use to difference between them. THis is the rule:
var.mdomain = "mydomain.com"
var.mdomainregexredir = "^mydomain\.com$"
$HTTP["host"] =~ var.mdomainregexredir {
server_name = var.mdomain
url.redirect = ( "^/(.*)" => "http://www." + var.mdomain + "/$1" )
}
If possible, I want to know if there's a way to use only one redirect rule, I mean, I don't like to add 2 url.redirect, one inside http and another inside https, I want it to be like this (Simplified):
url.redirect = ( "^/(.*)" => (isHttps?"https":"http") + "://www." + var.mdomain + "/$1" )
What you'll need is this:
$HTTP["host"] =~ var.mdomainregexredir {
$HTTP["scheme"] =~ ".*" { # catch http and https
# %0 references the above matched pattern (http or https)
# also changed to just catch the whole path and redirect using it with $0
url.redirect = ( ".*" => "%0://www.varmdomain$0" )
}
}
Unless you have a compelling reason, I'd be redirecting everything to https. You can do the https and www redirects all as one too.

lighttpd: 404 page does not return 404 status

I'm using a lighttpd 404 error handler, with a static 404 page. The entire conf file looks like this:
server.document-root = "/home/www/local/www/mysite/html"
url.rewrite = (
"^(.*)/($|\?.*)" => "$1/index.html",
"^(.*)/([^.?]+)($|\?.*)$" => "$1/$2.html"
)
server.error-handler-404 = "/404.html"
$HTTP["scheme"] == "http" {
url.redirect = ( "^/blog.html$" => "/blog/",
// various individual redirects
)
}
$HTTP["scheme"] == "https" {
$HTTP["url"] !~ "^/blog/admin/" {
url.redirect = ( "^/(.*)" => "http://www.mysite.com/$1" )
}
}
However, when I go to an address that should 404, I do correctly see our 404 page, but the status code is 200.
The lighttpd docs say that you should get a 404 status code if using a static page.
I think we're using a static page, but could something about the way we're redirecting mean that we're actually not?
Sorry for the newbie question.
Fixed this by using server.errorfile-prefix instead - think it is simply a bug in server.error-handler-404.
Seems to be a known bug in some versions of lighttpd. I was actually using a later version than 1.4.17, but still seeing the same problem.