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

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.

Related

Redirecting page to specific IP and port in lighttpd

I got 2 server under same network and I hope to redirect domain.com/page to 192.168.2.16:22348 so i got this in lighttpd.conf according to this: lighttpd as reverse-proxy
$HTTP["url"] =~ "^/page" {
proxy.server = ( "" => ( ( "host" => "192.168.2.16", "port" => 22348 ) ) )
}
but I ended up getting redirected to 192.168.2.16/maps which doesn't exist (I only got index.html but I can't change it) and returned 404.
Is there any way to only pass IP and port but not /page?
(I also tried domain.com/google to google.com by the same way but ended up to google.com/google)
Others I also tried:
https://redmine.lighttpd.net/projects/1/wiki/Docs_ModProxy
Others I also tried: https://redmine.lighttpd.net/projects/1/wiki/Docs_ModProxy
I think you should look at proxy.header "map-urlpath" on the same page.
So instead of using www.domain.com/page, i decided to use subdomain.domain.com.
Here's what I got.
$HTTP["host"] =~ "subdomain.domain.com" {
proxy.server = ( "" => ( ( "host" => "192.168.2.16", "port" => "22348" ) ) )
}
Since there is nothing after subdomain.domain.com/, it won't pass anything but only ip and port (192.168.2.16:22348) so I don't need to modify anything from the second machine. I think this is easier to configure instead of using /page.

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"
)

Nginx Multiple domains set redirect for specific domain

my nginx server supports multiple domains:
server {
server_name
domain1.com www.domain1.com
domain2.com www.domain2.com
domain3.com www.domain3.com
}
I would like to specify different redirection rules per domain, such as:
if ( $host ~ "domain1.com" ) {
map $request_uri $redirect_uri {
/about.html /about-us
}
}
if ( $host ~ "domain2.com" ) {
map $request_uri $redirect_uri {
/about.html /contact-us
}
}
nginx only allows if inside a server block and map outside of server block so i'm stuck, any ideas how to implement this?
Maybe i should include a redirections file per domain, something like:
$redirections_file = "redirections/" . $host;
if ( file_exists( $redirections_file ) ) {
include $redirections_file;
}
this is not a valid nginx code of course, just pseudo code.

Lighttpd - redirect a specific URL to another domain

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
}
}

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"
}
}