5

I want to redirect my www.mysite.in to https://www.mysite.in, I tried all possible code of php in index.php as below mentioned but still unable to redirect....I am not getting exact solution. Please help me regarding.

<?php
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != '') {
    header("location: https://".$_SERVER['HTTP_HOST']);
} else {
    header("location: http://".$_SERVER['HTTP_HOST'])
}?>
------------------------------------OR---------------------------------------
<?php

if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == ""){
    $redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: $redirect");
}
?>
------------------------------------OR----------------------------------------
<?php
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == "")
    {
        $HTTPURI = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        header("HTTP/1.1 301 Moved Permanently"); // Optional.
        header("Location: $HTTPURI"); 
        exit(0);
    }
?>

5
  • 1
    Are you able to modify/create a .htaccess file? If so try besthostratings.com/articles/force-ssl-htaccess.html Commented Mar 14, 2015 at 5:26
  • I have hosted my site on godaddy.com, I don't know how to create .htaccess file too...will you please help to guide about?. Commented Mar 14, 2015 at 5:30
  • You can create an .htaccess file by transferring a file to your server and renaming it; executing touch filename on the server; or by executing a number of commands in PHP to generate/create a file (file_put_contents, fwrite, etc.) Commented Mar 14, 2015 at 5:40
  • If you want to require only SSL connections (i.e. you don't want any users connecting via HTTP), you can always opt for HSTS headers. They aren't supported by a few older browsers or Internet Explorer currently, but the next version of IE is said to adopt it - so that's one way to add the redirect =] Commented Mar 14, 2015 at 6:02
  • I tried Vainglory07's code it work for me.... Thank you all for your help. Commented Mar 14, 2015 at 6:04

2 Answers 2

5

How about like this:

Have a function that will check the protocol of the website:

function is_https() {
    if (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 1) {
        return TRUE;
    } elseif (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on') {
        return TRUE;
    } else {
        return FALSE;
    }
}

Then if not, you do the redirect.

if (! is_https()) {
    header("location: https://{$_SERVER['HTTP_HOST']}");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a reason not to do this via .htaccess?
3

Better you can use .htaccess with the below code. Instead of putting manual php code in each page you can control one file the whole website.

# Switch rewrite engine on
RewriteEngine on
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# Do index.php to root redirect
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ https://%{HTTP_HOST}/$1 [R=301,L]

2 Comments

This is good idea. thumbs up. But i think the if he's using PHP framework, PHP approach to his problem doesn't need to call the script on every page. It can be called globally once on every request.
This is a bad idea for SEO purposes, this cause loop redirect in most cases

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.