Redirecting from Http to Https with PHP

Redirecting from HTTP to HTTPS with PHP

Try something like this (should work for Apache and IIS):

if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off") {
$location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $location);
exit;
}

php redirecting code http to https

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']}");
}

Laravel Redirect Http to Https

I solved with htaccess

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

PHP redirect to HTTPS if page is

Here You go.

//force the page to use ssl 
if ($_SERVER["SERVER_PORT"] != 443) {
$redir = "Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header($redir);
exit();
}

$_SERVER, It is an array containing information such as headers, paths, and script locations.

How to redirect http to https in codeigniter

Now I got a solution,
I updated my htaccess file.--

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC]
RewriteRule ^ https://www.myhost.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/
RewriteRule ^index\.php(/(.*))?$ myhost.com/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Now, It worked for me smoothly.. :)

How to redirect HTTP to HTTPS using XAMPP

I found the answer myself; I had vhosts setup and I had to place

<IfModule mod_rewrite.c>
RewriteEngine On

# Redirect /xampp folder to https
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} xampp
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [R,L]

# Redirect /phpMyAdmin folder to https
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} phpmyadmin
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [R,L]

# Redirect /security folder to https
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} security
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [R,L]

# Redirect /webalizer folder to https
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} webalizer
RewriteRule ^(.*) https://%{SERVER_NAME}$1 [R,L]
</IfModule>

Inside the directory I want redirected in C:/xampp/apache/config/extra/httpd-vhosts.conf.

This explained in the last few lines of robs notebook:

One thing to keep in mind with this redirection is that if you have virtual hosts, you need to place the redirection code (with the RewriteCond and RewriteRule) inside of your virtual host declarations, otherwise the redirection won’t work.



Related Topics



Leave a reply



Submit