
Hello there. You've paid your hosting provider and installed SSL. However, people are still able to access your site without SSL. This also applies to Google-Bot. If someone uses Google Chrome, your site is marked as 'Not Secure' and visitors may quickly leave. And of course, that can be frustrating — I’d be annoyed too. :)
So how can we enforce SSL access for users using PHP? Actually, it’s very easy to handle this using PHP. All you need to do is add the following code at the very top of your PHP file (usually index.php
), right after <?php:
GENEL
if(empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off'){
$https_url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $https_url);
exit();
}
That’s it. Since the code automatically uses your site's domain and request URI, no need to change anything manually.
If you have any questions, feel free to leave a comment below!
Related Articles

Reusable PHP Functions for Various Projects
0 Comments
Comments ()
No comments yet. Be the first to comment!