+3 votes
28 views

I'm working on a cms website and I would like to redirect all of my visitors from http to https. My Website also has both the versions http and https but I don't know how to redirect my all traffic to https. I want it be done in php. If anyone have solution, please help me.

by (1.4k points)  
retagged by | 28 views

1 Answer

+1 vote

Please try something like this


<?php
if (!(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')){
   $redirect = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
   //Headers
   header('HTTP/1.1 301 Moved Permanently');
   header('Location: ' . $redirect);
   //terminate execution
   exit();
}
?>

Hope it would work for you!!

by (3.3k points)  

Related questions

+1 vote
1 answer
asked Oct 4 by toheed (1.4k points) | 15 views
+1 vote
1 answer
+2 votes
0 answers
+2 votes
0 answers
+4 votes
1 answer
asked Mar 29 by admin (1.3k points) | 48 views
+4 votes
1 answer
asked Mar 20 by Ron | 48 views
+2 votes
0 answers
asked Jun 17 by abrishnoor (1.7k points) | 9 views
+2 votes
0 answers
asked Jun 20 by athar (3.3k points) | 19 views
+3 votes
1 answer
asked Jun 19 by athar (3.3k points) | 21 views




Welcome to Meansflow - Where Developers and Students Learn, Share, & Build Careers, where you can ask questions and receive answers from other members of the community.
118 questions
90 answers
43 comments
113 users