2

I operate a lighttpd webserver hosting Wordpress. For usability and marketing reasons I have registered a new domain that should replace the old one - site structure is unchanged. Wordpress setup is okay so far and works with the new domain.
Now I want to have all search engine results pointing to the old URL be redirected (301) by my Lighttpd server to the new url:
www.olddomain.xy/path/somepage.html should land on www.newdomain.xy/path/somepage.html
Here and elsewhere, I have found solutions that replace http with https, replace domain.xy with www.domain.xy or solutions that redirect any page from the old domain to the root directory of the new domain forgetting about the path. But this is not what I want.

I can't seem to wrap my head around the (regex-)syntax. Any ideas?

Chris Krz
  • 41
  • 6
  • Read the fine manual and look at the examples? https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModRedirect https://redmine.lighttpd.net/projects/lighttpd/wiki/HowToRedirectHttpToHttps – gstrauss Sep 07 '17 at 04:03
  • Hi, thanks for your help. Yes I read that before posting my question, but imho it doesn't cover what I need. It's all about getting from http://xyz.tld to http://www.xyz.tld. I tried: $HTTP["host"] =~ "^some.old.domain" { url.redirect = ( "^/(.*)" => "https://my.new.domain/$1" ) } however, that doesn't work – Chris Krz Sep 07 '17 at 12:15

2 Answers2

2

This somehow did the trick and also ensured to redirect to https:

$SERVER["socket"] == ":80" {
    $HTTP["host"] =~ "^www\.olddomain\.xyz" {
        url.redirect = ( "^/(.*)" => "https://www.newdomain.eu/$1/" )
    }
}
$SERVER["socket"] == ":443" {
    $HTTP["host"] =~ "^www\.olddomain\.xyz" {
        url.redirect = ( "^/(.*)" => "https://www.nwedomain.eu/$1/" )
    }
}

rgds Chris

Chris Krz
  • 41
  • 6
  • FYI: Given that your example appears to be looking for an exact match (assuming .xyz is the TLD), you could use an exact match in the config rather than a regex: $HTTP["host"] == "www.olddomain.xyz" – gstrauss Sep 09 '17 at 23:49
0

With the above solution, I tried to relocate my old domain via Google's Search Console. However,they could't recognize my 301 setup. So I went a completely diifferent route by adding the 301 and https redirection into Wordpress' index.php file:

<?php
//check called domain - subdomain never varies
$domnam = $_SERVER['SERVER_NAME'];
if ($domnam == "chriskrz.selfhost.bz") {
        // permanent PHP-Redirect (Statuscode 301)
        header("HTTP/1.1 301 Moved Permanently" );
        // Redirection target
        header("Location: HTTP://www.rustimation.eu" );
        // to go sure an exit statement in case of errors
        exit;
    }
//force redirect to secure page
if($_SERVER['SERVER_PORT'] == '80')
        { header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
        exit();
        }

This is accepted by Google and works like intended.

Chris Krz
  • 41
  • 6