Skip to main content
ubuntuask.com

Back to all posts

How to Set All Link to Https Using Php Or .Htaccess?

Published on
4 min read
How to Set All Link to Https Using Php Or .Htaccess? image

Best Tools to Secure Your Site to Buy in November 2025

1 Lantronix PXN210002-01U PremierWave XN Device server 4 port 10Mb/100Mb LAN, RS-232, USB 2.0, 802.11 b/a/g/n

Lantronix PXN210002-01U PremierWave XN Device server 4 port 10Mb/100Mb LAN, RS-232, USB 2.0, 802.11 b/a/g/n

  • EXPERIENCE LIGHTNING-FAST DUAL-BAND 802.11N WIRELESS SPEEDS!
  • ENJOY SEAMLESS MOBILE CONNECTIVITY WITH LANTRONIX SMARTROAM TECH!
  • SECURE YOUR DATA WITH ENTERPRISE-LEVEL AES, SSL, AND SSH ENCRYPTION!
BUY & SAVE
$214.90
Lantronix PXN210002-01U PremierWave XN Device server 4 port 10Mb/100Mb LAN, RS-232, USB 2.0, 802.11 b/a/g/n
2 Synology 2-Bay NAS DS223 (Diskless)

Synology 2-Bay NAS DS223 (Diskless)

  • 100% DATA OWNERSHIP WITH SEAMLESS MULTI-PLATFORM ACCESS.
  • EFFORTLESSLY SHARE AND SYNC FILES FOR CLIENT COLLABORATION.
  • RELIABLE BACKUP OPTIONS ENSURE YOUR DATA IS ALWAYS PROTECTED.
BUY & SAVE
$284.99
Synology 2-Bay NAS DS223 (Diskless)
3 Rain Barrel Spigot Kit 2 Pack High Flow, 3/4" GHT Heavy Duty Plastic Water Barrel Spigot Kit with Bulkhead Fitting, Water Rain Barrel Diverter Kit for Outdoor Plastic Bucket (45 degrees)

Rain Barrel Spigot Kit 2 Pack High Flow, 3/4" GHT Heavy Duty Plastic Water Barrel Spigot Kit with Bulkhead Fitting, Water Rain Barrel Diverter Kit for Outdoor Plastic Bucket (45 degrees)

  • COMPLETE KIT FOR VERSATILE USE WITH ALL NECESSARY FITTINGS INCLUDED.
  • HEAVY-DUTY PLASTIC ENSURES LONG-LASTING OUTDOOR PERFORMANCE.
  • DOUBLE WASHERS PROVIDE ENHANCED LEAK PROTECTION FOR PEACE OF MIND.
BUY & SAVE
$8.99
Rain Barrel Spigot Kit 2 Pack High Flow, 3/4" GHT Heavy Duty Plastic Water Barrel Spigot Kit with Bulkhead Fitting, Water Rain Barrel Diverter Kit for Outdoor Plastic Bucket (45 degrees)
4 MONIGEAR Network Temperature Humidity Monitor, AWS Azure loT Sensor, Support Multiple protocols with SSL: MQTT, BACnet, SNMP, Modbus TCP, PoE Power Supply (6 Packs)

MONIGEAR Network Temperature Humidity Monitor, AWS Azure loT Sensor, Support Multiple protocols with SSL: MQTT, BACnet, SNMP, Modbus TCP, PoE Power Supply (6 Packs)

  • VERSATILE PROTOCOL SUPPORT: WORKS WITH MODBUS, SNMP, BACNET, MQTT.
  • CONVENIENT POE POWER SUPPLY: SIMPLIFIED WIRING WITH UNINTERRUPTED POWER.
  • CERTIFIED QUALITY ASSURANCE: FCC, CE-EMC, ROHS CERTIFIED FOR MARKET TRUST.
BUY & SAVE
$431.94
MONIGEAR Network Temperature Humidity Monitor, AWS Azure loT Sensor, Support Multiple protocols with SSL: MQTT, BACnet, SNMP, Modbus TCP, PoE Power Supply (6 Packs)
5 Synology 1-Bay DiskStation DS124 (Diskless) Black

Synology 1-Bay DiskStation DS124 (Diskless) Black

  • CENTRALIZED DATA HUB FOR COMPLETE OWNERSHIP AND MULTI-PLATFORM ACCESS.
  • EFFORTLESS COLLABORATION WITH SEAMLESS SHARING AND SYNCING ACROSS DEVICES.
  • SECURE YOUR FILES WITH BUILT-IN PROTECTION AND FLEXIBLE BACKUP OPTIONS.
BUY & SAVE
$138.99 $179.99
Save 23%
Synology 1-Bay DiskStation DS124 (Diskless) Black
6 Smart Light Switch, Compatible with Alexa, Google Home No Hub Required, Smart Home WiFi Wireless Fit for 1/2/3/4 Gang Switch Box, Neutral Wire Need Micmi MK36, Smart Light Switch

Smart Light Switch, Compatible with Alexa, Google Home No Hub Required, Smart Home WiFi Wireless Fit for 1/2/3/4 Gang Switch Box, Neutral Wire Need Micmi MK36, Smart Light Switch

  • AUTOMATE DEVICES WITH SMART SCHEDULE TIMER FOR ENERGY SAVINGS.
  • CONTROL DEVICES EFFORTLESSLY WITH VOICE COMMANDS VIA ALEXA/GOOGLE.
  • REMOTE ACCESS VIA SMART LIFE APP FOR ULTIMATE CONVENIENCE ANYTIME.
BUY & SAVE
$18.99
Smart Light Switch, Compatible with Alexa, Google Home No Hub Required, Smart Home WiFi Wireless Fit for 1/2/3/4 Gang Switch Box, Neutral Wire Need Micmi MK36, Smart Light Switch
7 Synology 4-Bay DiskStation DS423 (Diskless) Black

Synology 4-Bay DiskStation DS423 (Diskless) Black

  • ACCESS & SHARE FILES SECURELY FROM ANYWHERE WITH EASE.
  • COMPREHENSIVE DATA BACKUP TO SAFEGUARD AGAINST MALWARE THREATS.
  • VERSATILE VIDEO SURVEILLANCE FOR HOMES/BUSINESSES (IP CAMERA LICENSE NEEDED).
BUY & SAVE
$379.99 $399.99
Save 5%
Synology 4-Bay DiskStation DS423 (Diskless) Black
8 Amcrest 5MP AI Turret IP PoE Camera w/ 49ft Nightvision, Security IP Camera Outdoor, Built-in Microphone, Human & Vehicle Detection, Active Deterrent, 129° FOV, 5MP@20fps IP5M-T1277EW-AI

Amcrest 5MP AI Turret IP PoE Camera w/ 49ft Nightvision, Security IP Camera Outdoor, Built-in Microphone, Human & Vehicle Detection, Active Deterrent, 129° FOV, 5MP@20fps IP5M-T1277EW-AI

  • AI DETECTION & DUAL ILLUMINATION: ACCURATE ALERTS WITH NIGHT VISION.

  • REMOTE ACCESS & PLAYBACK: MANAGE FOOTAGE EASILY VIA YOUR SMARTPHONE.

  • SECURE OFF-SITE BACKUP: PROTECT FOOTAGE WITH AMCREST CLOUD STORAGE.

BUY & SAVE
$69.99
Amcrest 5MP AI Turret IP PoE Camera w/ 49ft Nightvision, Security IP Camera Outdoor, Built-in Microphone, Human & Vehicle Detection, Active Deterrent, 129° FOV, 5MP@20fps IP5M-T1277EW-AI
9 SonicWall Global VPN Client - License - 5 Licenses (01-SSC-5316) - Secure IPsec VPN Connectivity for Remote Work & Site-to-Site Access

SonicWall Global VPN Client - License - 5 Licenses (01-SSC-5316) - Secure IPsec VPN Connectivity for Remote Work & Site-to-Site Access

  • SECURE IPSEC VPN ACCESS FOR ENCRYPTED REMOTE CONNECTIONS.
  • RELIABLE, HIGH-PERFORMANCE VPN EXPERIENCE FOR REMOTE EMPLOYEES.
  • SIMPLE WINDOWS COMPATIBILITY ENSURES EASY INSTALLATION AND SETUP.
BUY & SAVE
$215.00
SonicWall Global VPN Client - License - 5 Licenses (01-SSC-5316) - Secure IPsec VPN Connectivity for Remote Work & Site-to-Site Access
+
ONE MORE?

To set all links to use HTTPS in PHP, you can use the following code snippet:

if($_SERVER['HTTPS'] != 'on'){ header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); exit; }

In the .htaccess file, you can use the following code snippet to force HTTPS on all links:

RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This code will redirect all your HTTP links to HTTPS, ensuring that all the links on your website are secure.

To automatically redirect all links to HTTPS in PHP, you can add the following code snippet at the beginning of your PHP files:

if($_SERVER['HTTPS'] != 'on') { $redirect = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; header('Location: ' . $redirect); exit(); }

This code checks if the current protocol is not HTTPS, and if so, redirects the user to the HTTPS version of the current page.

If you prefer to use .htaccess to automatically redirect all links to HTTPS, you can add the following code snippet to your .htaccess file:

RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This code will check if the current protocol is not HTTPS, and if so, redirect the user to the HTTPS version of the current page with a 301 permanent redirect.

Either of these solutions will automatically redirect all links to HTTPS on your website.

The best practice for updating all links to HTTPS in PHP is to create a function that checks if the current URL is using HTTP and then redirects to the HTTPS version. Here's an example function you can use:

function forceSSL() { if($_SERVER['HTTPS'] != 'on') { $url = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; header('Location: ' . $url, true, 301); exit(); } } forceSSL();

You can include this function at the beginning of your PHP files to ensure all links are redirected to HTTPS.

In .htaccess, you can use the following code to redirect all HTTP requests to HTTPS:

RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Place this code in your .htaccess file in the root directory of your website to automatically redirect all HTTP requests to HTTPS.

To update all links to use HTTPS in PHP, you can use the following code snippet to replace "http://" with "https://" in all links:

$html = file_get_contents('example.html'); $html = str_replace('http://', 'https://', $html); file_put_contents('example.html', $html);

This code will read the content of an HTML file (example.html), replace all occurrences of "http://" with "https://", and then write the updated content back to the file.

Alternatively, you can use the following code to redirect all HTTP requests to HTTPS in your .htaccess file:

RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This code will check if the request is not using HTTPS and then redirect it to the HTTPS version of the same URL. Make sure to place this code at the beginning of your .htaccess file.

The most reliable way to switch all links to https in PHP or .htaccess is by using a combination of both methods.

In .htaccess, you can use the following code to redirect all HTTP requests to HTTPS:

RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This code checks if the request is not using HTTPS and then redirects to the HTTPS version of the URL.

Additionally, in your PHP code, you can use the following function to generate HTTPS URLs:

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

forceHTTPS();

This code checks if the request is not using HTTPS and then redirects to the HTTPS version of the URL.

By using a combination of .htaccess and PHP, you can ensure that all links on your website are switched to HTTPS for better security and SEO benefits.