Project

General

Profile

[Solved] Redirect example.com to www.example.com and http to https

Added by golinux almost 3 years ago

I have 2 sites hosted on 1 server with lighttpd on Ubuntu 20.04

I want url redirect (or rewrite) from example.com to www.example.com and http:// to https://

On this support forum I only found the opposit redirect, i.e. from www.example.com to example.com

but I think that www.example.com is a better way to display the url, most big company like google, use www.

This is my lighttpd.config: (I have ssl and fastcgi enabled) ==========================================================

server.modules = (
    "mod_expire",
    "mod_access",
    "mod_alias",
    "mod_compress",
    "mod_redirect",
    "mod_rewrite",
    "mod_setenv",
    "mod_indexfile",
)

server.document-root        = "/var/www/html" 
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log" 
server.pid-file             = "/run/lighttpd.pid" 
server.username             = "www-data" 
server.groupname            = "www-data" 
server.port                 = 80
url.rewrite-once = ( "^(.*)/$" => "$1/" )
url.rewrite-if-not-file = ( "^([^?]*)(\?.*)?$" => "$1.php$2" )

# features
#https://redmine.lighttpd.net/projects/lighttpd/wiki/Server_feature-flagsDetails
server.feature-flags       += ("server.h2proto" => "enable")
server.feature-flags       += ("server.h2c"     => "enable")
server.feature-flags       += ("server.graceful-shutdown-timeout" => 5)
#server.feature-flags       += ("server.graceful-restart-bg" => "enable")

# strict parsing and normalization of URL for consistency and security
# https://redmine.lighttpd.net/projects/lighttpd/wiki/Server_http-parseoptsDetails
# (might need to explicitly set "url-path-2f-decode" = "disable" 
#  if a specific application is encoding URLs inside url-path)
server.http-parseopts = (
  "header-strict"           => "enable",# default
  "host-strict"             => "enable",# default
  "host-normalize"          => "enable",# default
  "url-normalize-unreserved"=> "enable",# recommended highly
  "url-normalize-required"  => "enable",# recommended
  "url-ctrls-reject"        => "enable",# recommended
  "url-path-2f-decode"      => "enable",# recommended highly (unless breaks app)
 #"url-path-2f-reject"      => "enable",
  "url-path-dotseg-remove"  => "enable",# recommended highly (unless breaks app)
 #"url-path-dotseg-reject"  => "enable",
 #"url-query-20-plus"       => "enable",# consistency in query string
)

index-file.names            = ( "index.php", "index.html" )
url.access-deny             = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

# default listening port for IPv6 falls back to the IPv4 port
## Use ipv6 if available
#include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.conf.pl" 
include "/etc/lighttpd/conf-enabled/*.conf" 

#server.compat-module-load   = "disable" 
server.modules += (
    "mod_dirlisting",
    "mod_staticfile",
)

$HTTP["host"] =~ "(^|.)bookdirectapp.com$" {
    server.name = "bookdirectapp.com" # Domain Name OR Virtual Host Name
    server.document-root = "/var/www/bookdirectapp.com" 
    server.errorlog = "/var/log/lighttpd/bookdirectapp.com-error.log" 
    accesslog.filename = "/var/log/lighttpd/bookdirectapp.com-access.log" 
    ssl.pemfile = "/etc/letsencrypt/live/bookdirectapp.com/web.pem" # Combined Certificate
    ssl.ca-file = "/etc/letsencrypt/live/bookdirectapp.com/chain.pem" # Root CA
}

$HTTP["host"] =~ "(^|.)roomswapping.com$" {
    server.name = "roomswapping.com" # Domain Name OR Virtual Host Name
    server.document-root = "/var/www/roomswapping.com" 
    server.errorlog = "/var/log/lighttpd/roomswapping.com-error.log" 
    accesslog.filename = "/var/log/lighttpd/roomswapping.com-access.log" 
    ssl.pemfile                 = "/etc/letsencrypt/live/roomswapping.com/web.pem" 
    ssl.ca-file                 = "/etc/letsencrypt/live/roomswapping.com/chain.pem" 
}

$HTTP["host"] =~ "^www\.(.*)$" {
  url.redirect  = (
    "^/(.*)" => "http://%1/$1",
  )
}

$HTTP["scheme"] == "http" {
            url.redirect = ("" => "https://${url.authority}${url.path}${qsa}")
}


Replies (12)

RE: Redirect example.com to www.example.com and http to https - Added by gstrauss almost 3 years ago

So what are you asking? Are you asking how to change the regex in the following?

$HTTP["host"] =~ "^www\.(.*)$" {
  url.redirect  = (
    "^/(.*)" => "http://%1/$1",
  )
}

What did you try? What regexes did you try?

RE: Redirect example.com to www.example.com and http to https - Added by golinux almost 3 years ago

I was asking for the right regex or other solution for redirect example.com to www.example.com

Now in my config I have the reverse redirect, i.e. from www.example.com to example.com with this code

$HTTP["host"] =~ "^www\.(.*)$" {
  url.redirect  = (
    "^/(.*)" => "http://%1/$1",
  )
}

How can I have my site always with www.example.com ?

RE: Redirect example.com to www.example.com and http to https - Added by gstrauss almost 3 years ago

I repeat: What did you try? What regexes did you try?

(I do not believe that you have tried anything. I do believe you are looking for someone to spoon-feed you. Try something and ask a more specific question if you have trouble.)

RE: Redirect example.com to www.example.com and http to https - Added by golinux almost 3 years ago

My question is simple, I would redirect all requests mysite.com* to www.mysite.com*

I am new in lighttpd, I always used Apache with mod_rewrite and .htaccess

I looked for all day on google for a solution, but I only found the opposite solution, a regex than
redirect www.mysite.com to mysite.com

It is very strange that no people have the same my needs.
Many big company use this redirect, for example google.com or youtube.com redirect to www.google.com and www.youtube.com respectively

RE: Redirect example.com to www.example.com and http to https - Added by gstrauss almost 3 years ago

The reason I ask people to make and show some effort is because I have found that giving answers to entities that can produce sounds without engaging their brains simply leads to more sounds from those entities, and still without engaging their brains.

It is very strange that no people have the same my needs.

There are multiple ways to do what you are asking, starting with the trivially simple that anyone who made a basic effort would likely stumble upon. lighttpd configuration documentation

RE: Redirect example.com to www.example.com and http to https - Added by golinux almost 3 years ago

I put a lot of effort to find a solution on google, I read also the lighttpd configuration documentation, but I didn't find a solution for my problem. Yesterday I spent all day to search on the internet, and at the end I open this thread on this forum, if you see the time of my message it is 2:15 of some timezone, but in Italy, my country, it was in the middle of the night i.e. 4.15 a.m. after that I went to sleep.

RE: Redirect example.com to www.example.com and http to https - Added by gstrauss almost 3 years ago

I believe you that you didn't find the direct, literal answer.

I wrote above: "The reason I ask people to make and show some effort ..." (emphasis mine)

So, for the third time, I ask once again: "What did you try? What regexes did you try?"

RE: Redirect example.com to www.example.com and http to https - Added by golinux almost 3 years ago

I tryed a lot of regexes, I tryed to modify someone that I found on internet, but without success.

For Example i modified this one:

$HTTP["host"] =~ "^www\.(.*)$" {
url.redirect = (
"^/(.*)" => "http://%1/$1",
)
}

in this way:

$HTTP["host"] =~ "^\.(.*)$" {
url.redirect = (
"^/(.*)" => "http://www.%1/$1",
)
}

And I got an infinite loop, and Firefox notice me an error.
I also tryed other variant, now I can list all, but no one works.

I repeat it is very crazy that no one wrote the direct literal answer to my problem.
Also the lighttpd documentation do not cover my case.
I only found a redirect from http to https

I repeat, to rewrite url from mysite.com to www.mysite.com is a very common for many website, on apache and nginx it is easy to find how to do it on official documentation.

RE: Redirect example.com to www.example.com and http to https - Added by gstrauss almost 3 years ago

I repeat it is very crazy that no one wrote the direct literal answer to my problem.

No, not crazy. You are frustrated, and that is understandable. However, you are blind to your severe limitations. That is less excusable.

The reason you have not seen a direct solution is that it is brain-dead simple.

As I said above, there are many solutions, some so trivial that you should consider hiring someone to help you since you could not figure out even the most simple solution of directly matching the host name(s) you wanted to redirect.

$HTTP["host"] == "bookdirectapp.com" {
  url.redirect = ("" => "https://www.bookdirectapp.com${url.path}${qsa}")
}
$HTTP["host"] == "roomswapping.com" {
  url.redirect = ("" => "https://www.roomswapping.com${url.path}${qsa}")
}

Now, many professional sites use subdomains such as static.example.com in addition to example.com and www.example.com, so while you might want to redirect everything that is not www.example.com to www.example.com, many other people do not want to do that.

For someone like yourself with two sites, the direct match approach fits nicely with your existing conditions

$HTTP["host"] =~ "(^|.)bookdirectapp.com$" {
    # ... 
    $HTTP["host"] == "bookdirectapp.com" {
        url.redirect = ("" => "https://www.bookdirectapp.com${url.path}${qsa}")
    }
}

There many other solutions available. This is not the only one, but is the trivial one I have chosen to present here.

RE: Redirect example.com to www.example.com and http to https - Added by golinux almost 3 years ago

thank you so much!!!!!!!!!!!!!!!!!

I also tryed your solutions without success, maybe I was wrong with some small error that made it not work.

I do not use subdomains at moment, but my idea is to redirect bookdirectapp.com to www.bookdirectapp.com, it is not necessary to redirect all subdomains *.bookdirectapp.com

RE: Redirect example.com to www.example.com and http to https - Added by gstrauss almost 3 years ago

FYI: the word is "tried", not "tryed". (There are lots of exceptions in English.)

I also tryed your solutions without success, maybe I was wrong with some small error that made it not work.

Since it appears like you are running a business, I strongly suggest that you hire someone local to help you. You are demonstrably not very good at reading and understanding the general programming concepts of conditionals, or in using regular expressions.

RE: Redirect example.com to www.example.com and http to https - Added by gstrauss almost 3 years ago

I put a lot of effort to find a solution on google, I read also the lighttpd configuration documentation, but I didn't find a solution for my problem. Yesterday I spent all day to search on the internet, and at the end I open this thread on this forum, if you see the time of my message it is 2:15 of some timezone, but in Italy, my country, it was in the middle of the night i.e. 4.15 a.m. after that I went to sleep.

The canonical documentation for lighttpd is on this site. mod_redirect is a short page that contains examples. If you check the history of the page mod_redirect, the example answering your question has been on that page since 2006 (~15 years ago)

It is crazy that you (supposedly) spent so much time and failed to find an answer.

    (1-12/12)