Yupage
Yupage

Reputation: 21

Change Https to Http in a link for all subdomain using javascript or php

i need a script (JavaScript or PHP not mod_rewrite) which can replace all HTTPS to HTTP on all *subdomain.example.com link but keep HTTPS for example.com and www.example.com.

Eg: i want to turn

<a href="HTTPS://sub.example.com">My sub domain</a>

to

<a href="HTTP://sub.example.com">My sub domain</a>

*There will be lot of different sub-domain...

Upvotes: 1

Views: 480

Answers (3)

grabantot
grabantot

Reputation: 2149

Solution that doesn't require jQuery:

var as = document.getElementsByTagName('a')      //get all a tags
var re = /^https:\/\/[\w\W]*(example.com)/i       //http://*example.com
var reExcept = /^https:\/\/(www.)?(example.com)/i //filter https://www.example.com and http://example.com

for (var i=0; i<as.length; i++) {
    href = as[i].getAttribute('href')
    console.log('original href: ' + href)
    if (!href || !re.test(href) || reExcept.test(href) )
        continue                                   //this href shouldn't be replaced
    href = href.replace('https://', 'http://')
    as[i].setAttribute('href', href)
    console.log('replaced href: ' + as[i].getAttribute('href'))
}

Tested through console at https://www.google.com/search?q=google (with google.com instead of example.com in re and reExept). Seems to work fine.

A bit less wordy version:

var re = /^https:\/\/[\w\W]*(example.com)/i
var reExcept = /^https:\/\/(www.)?(example.com)/i
var as = document.getElementsByTagName('a')
for (var i=0; i<as.length; i++) {
    href = as[i].getAttribute('href')
    if ( !href  || !re.test(href) || reExcept.test(href) )
        continue
    as[i].setAttribute('href', href.replace(/^https:\/\//i, 'http://'))
}

Upvotes: 1

hherger
hherger

Reputation: 1680

Here's a solution in PHP. It also covers sub-sub domains like subsub.sub.example.com and domain names only like example.com.

Be aware that when you want to do it on the server side…

  • The script does a redirect (there are no alternatives) to change the protocol from https to http.
  • An encrypted connection with SSL / HTTPS is established before a (php) script can get control.
  • So, you can not use a server-side solution for cases where (e.g.) a certificate does not match some subdomain names, while others are matched.

    <?php
    $tochange = array(
        'sub', 'sub1', 'test',
    );
    
    $hname = getenv("SERVER_NAME");
    if (preg_match('/^(.*)(\.(.*?)\.(.*))$/s', $hname, $regs)) {
        // $regs[1] contains subdomain name / names (also: *subsub.sub.domain.tld").
        $encrypted = ((isset($_SERVER["HTTPS"])) && (strtoupper($_SERVER["HTTPS"]) == 'ON'));
        if ($encrypted && (in_array($regs[1],$tochange))) {
            $port = getenv("SERVER_PORT");
            $query = ($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '';
            $url = 'httpx://' . $hname . $_SERVER['SCRIPT_NAME'] . $query;
            // Do a redirect
            header("location: $url");  // redirect
            // echo "$url<br >";       // for testing
        }
    }
    ?>
    

Upvotes: 0

Prem
Prem

Reputation: 1447

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<a href="https://www.test.com" >Test 1 </a><br>
<a href="https://ftp.test.com" >Test 2 </a><br>
<a href="https://test.test.com" >Test 3 </a><br>


<script type="text/javascript">
function extractDomain(url) {
    var domain;
    //find & remove protocol (http, ftp, etc.) and get domain
    if (url.indexOf("://") > -1) {
        domain = url.split('/')[2];
    }
    else {
        domain = url.split('/')[0];
    }

    //find & remove port number
    domain = domain.split(':')[0];
    domain = domain.replace("www.", "");
    return domain;
}

$("a").each(function(){
  var url = $(this).attr("href");
  var res = extractDomain( url );
  var resSplit = res.split('.');
  if( resSplit.length > 2 ){
    $(this).attr("href", url.replace("https","http") );
  }

});

</script>

Upvotes: 0

Related Questions