Force HTTP requests to HTTPS using .htaccess

This article will show you how you can force your website’s HTTP requests to HTTPS using htaccess.

Video tutorial

What is htaccess ?

.htaccess is a file used for server configuration. It is used primarily for URL redirection, preventing access to specific files, etc.

Force HTTP requests to HTTPS using .htaccess

If you have a website that is by default loads on HTTP, then this article is for you.

First, you need to open your .htaccess file from the root of your project.

If you do not have .htaccess file at the root, you need to manually create it in your main folder. Make sure to add the “.” dot at the beginning.

Then you need to write the following re-write condition and rule at the end of your .htaccess file.

# Redirect all http traffic to https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://adnan-tech.com/$1 [R,L]

By default, the HTTP server loads on port 80. So, we have set the condition to port 80. Whatever comes to port 80, will be passed to the re-write rule.

Our re-write rule says that whatever comes in the URL of this website, will be redirected to the HTTPS address.

  • ^ means the start of the string.
  • $ means the end of the string.
  • () parenthesis is used for grouping in the regular expression.
  • . means 0 or more characters.
  • * means any character.
  • $1 means the URL will be passed as an argument.
  • [R,L] means to prevent further execution of rewrite rules. Make sure it does not have any space.

So that’s how you can force HTTP requests to HTTPS using .htaccess. If you face any problems in following this, kindly do let me know.

Copy entire folder from one storage to another – Laravel

In this tutorial, we are going to show you, how you can copy or move a folder from one storage to another storage folder in Laravel.

Video tutorial

So let’s say in your storage/app/public folder, these folders are already created by Laravel by default. We created 2 folders named folder1 and folder2. In folder1, we have a folder named files and it has some files. Now you want to move the entire files folder with all his files in folder2.

  • storage/app/public
    • folder1
      • files
    • folder2

Copy folder from one storage to another – Laravel

Following will be the code that will copy the entire folder from one storage to another:

// include classes
use Storage;
use Str;

// get all files from source folder
$files = Storage::files("public/folder1/files");

// loop through each file
foreach ($files as $file)
{
    // to get the content the file, we need to get it from public/storage
    // so change the path to public/storage folder
    $file = Str::of($file)->replace("public/folder1/", "public/storage/folder1/");

    // get the content of file
    $file_content = file_get_contents($file);

    // extract the file name from path
    $file_name_parts = explode("/", $file);
    if (count($file_name_parts) > 0)
    {
        // name is at the last index of array
        $file_name = $file_name_parts[count($file_name_parts) - 1];

        // set destination path
        $file_path = "public/folder2/files/" . $file_name;

        // save the file in destination folder
        Storage::put($file_path, $file_content);
    }
}

// delete the source folder
// do this only if you want to move the folder
Storage::deleteDirectory("public/folder1/files");

Explanation

  1. First, we are going to get all the files from the source folder.
  2. Then we will loop through each file.
  3. Then we need to get the content of each file. But in order to get the content of the Laravel storage file, we need to access it from a public shortcut path.
  4. So to change the path, we are going to use another class named Str.
  5. Then we need to call the function of and tell the target string and call the replace function. We will replace the “public/folder1/” (notice slash at the end) with the “public/storage/folder1/”. And save it back in the $file variable.
  6. Now we can get the content of the file by calling the PHP native function file_get_contents and pass the path of the file, and save it in the variable $file_content. If you get any error here, you need to run the following command at the root of your project:
    • php artisan storage:link
  7. Then we need to extract the file name from the file path. First, we will split the file path by slash using PHP explode function. It will return an array and we need to get the last index of this array. So we will check if the array is not empty. Then we will get the last index of the array. So if the length of the array is 5, minus 1 becomes 4, so it will fetch the element at index 4 which is the name of the file.
  8. After that, we are setting the destination path. It will be in the public/folder2/files and the name of the file. Right now, folder2 does not have a files folder, but it will automatically create one. To save the file, we need to call the Storage::put method. Pass the destination path and the content of the file.
  9. In order to have the move functionality, you simply need to remove the source folder after it has been copied. So after the foreach loop, we will call the deleteDirectory method from the Storage class and pass the path of the source folder.
  10. If you run the code now, you will see that folder1 becomes empty but now folder2 has all the files.

If you are working on AJAX and are getting any errors, feel free to check our tutorial on how to check Laravel errors from AJAX requests.

So that’s how you can copy or move an entire folder from one storage to another in Laravel. If you face any problems in following this, kindly do let me know.

Hosting email address send SMTP email in PHP

In this tutorial, we will teach you how to send email from a hosting email address using SMTP in PHP. Recently, Google has disabled its “less secure apps” option. This means that you can no longer send SMTP emails from your Gmail account.


So, today we are going to show you, how you can send SMTP emails from your own hosting email address.

Step 1: Login to cPanel

First step, is to login to your hosting provider and open cPanel. In your cPanel, search for “email” and goto “Email accounts”.

Step 2: Search email address

Then search for desired email address, from which you want to send emails and select “Check email”.

Step 3: Open mail client configurations

On the next page, you need to look for the section “Other webmail features” and click on the “Configure mail clients” box.

Step 4: Check SMTP details of your hosting email address

From here, you need to loop for the following configurations.

Step 5: Send SMTP email

Now, we need to install a library called PHPMailer. Open the command prompt at the root of your project and run the following command:

> composer require phpmailer/phpmailer

Make sure you have downloaded the composer from here. Write the following code in the file from where you want to send the email:

// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try
{
    //Enable verbose debug output
    $mail->SMTPDebug = 0; // SMTP::DEBUG_SERVER;

    //Send using SMTP
    $mail->isSMTP();

    //Set the SMTP server to send through
    $mail->Host = "mail host address";

    //Enable SMTP authentication
    $mail->SMTPAuth = true;

    //SMTP username
    $mail->Username = "email address";

    //SMTP password
    $mail->Password = "password";

    //Enable TLS encryption;
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

    //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
    $mail->Port = 587;

    //Recipients
    $mail->setFrom("email address", "your name");

    //Add a recipient
    $mail->addAddress("recipient email address", "recipient name");

    //Set email format to HTML
    $mail->isHTML(true);

    $mail->Subject = "subject";
    $mail->Body    = "body";

    $mail->send();
    echo 'Message has been sent';
}
catch (Exception $e)
{
    die("Message could not be sent. Mailer Error: " . $mail->ErrorInfo);
}

So that’s it. That’s how you can send emails from your hosting email address using SMTP in PHP. If you want to attach files with the email, we have already created a separate tutorial on that. If you face any problems in following this, kindly do let me know.