• I am running a multisite WoprdPress install where I have configured my install to use sub-domains.
    I have registered and added one domain domain-a.co.za which is my main site.
    I have recently registered two main domains, domain-a.co.za and domain-c.co.za, and I have pointed their DNS records IP address of my multisite.
    I need domain-b and domain-c to redirect to a category in domain-a.
    i.e:
    https://domain-b.co.za and https://domain-c.co.za
    redirect to:
    https://domain-a/category/sport/
    The first option I tried was creating a second site in my multisite network and mapped the two domains to the site. Then I activated a theme. In the theme I used PHP redirect code to redirect to the category in domain-a like this:

    <?php
    if($_SERVER['HTTP_HOST'] == 'domain-b.co.za' || $_SERVER['HTTP_HOST'] == 'domain-c.co.za')
    {
    	header("Location: https://domain-a.co.za/category/sport/");
    	exit;
    }
    
    ?>

    That resulted in a hit and miss in a sense that sometimes the redirection would work and sometimes it wouldn’t work.
    Then I replaced the the PHP redirect code with JavaScript like this:

    <html>
    <head>
    <script>
    function gotoPage()
    {
        var dns1 = "domain-b.co.za";
        var dns2 = "domain-c.co.za";
        var urlloc = location.href;
        var loc = urlloc.toLowerCase();
    
        if (loc.indexOf(dns1) != -1){
        	location.href="https://domain-a.co.za/category/sport/";
        }
       else if (loc.indexOf(dns2) != -1){
        	location.href="https://domain-a.co.za/category/sport/";
       }
    }
    </script>
    <body onLoad="gotoPage()">
    </body>
    </html>

    That also resulted in hit and miss. Sometimes the redirection would work and sometimes it would not work.
    Then I tried htaccess redirect like this:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^domain-b.co.za$
    RewriteRule ^$ https://domain-a.co.za/category/sport/$1 [L,R=301]
    RewriteRule ^(server-info|server-status) - [L]
    RewriteRule ^index\.php$ - [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
    RewriteCond %{REQUEST_URI} !^/php5.fastcgi/*
    RewriteRule ^(.*\.php)$ $1 [L]
    RewriteCond %{REQUEST_URI} !^/php5.fastcgi/*
    RewriteRule . index.php [L]
    </IfModule>
    # END WordPress

    That did not work at all. I am at a loss and I do not know what I need to do for the redirection to work. I would really appreciate any feedback.
    Oh, I need to mention that I am using Varnish Cache.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Rule #1 about .htaccess: do NOT edit the WP one. Seriously. Leave it alone.

    Rule #2: WordPress’ block always goes at the bottom.

    What you want is a simple http host check and redirect

    RewriteCond %{HTTP_HOST} ^domain-b\.co\.za
    RewriteRule ^(.*) https://domain-a/category/sport/$1 [L,R=301]
    Thread Starter kirsten-SAdev

    (@kirsten-sadev)

    Thanks for the feedback Mika. I really appreciate it.

    If my htaccess looks like below, where exactly do I put the http host check and redirect code?

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(server-info|server-status) - [L]
    RewriteRule ^index\.php$ - [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
    RewriteCond %{REQUEST_URI} !^/php5.fastcgi/*
    RewriteRule ^(.*\.php)$ $1 [L]
    RewriteCond %{REQUEST_URI} !^/php5.fastcgi/*
    RewriteRule . index.php [L]
    </IfModule>
    
    AuthUserFile /home/wordpress/public_html/wp-admin/.htpasswd
    AuthName "Citizen CMS Auth Required"
    AuthType Basic
    <Files "wp-login.php">
      require valid-user
    </Files>
    
    # END WordPress
    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Your .htaccess is … all jumbled up. You have lines like this:

    RewriteCond %{REQUEST_URI} !^/php5.fastcgi/*

    None of those were ever default WP and implies you have a rather complex and custom server setup.

    Put the code ABOVE # BEGIN WordPress at the very least.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Redirecting from domain to category’ is closed to new replies.