DEV Community

Cover image for Stop Hacking Your .htaccess: A Better Way to Switch Laravel Versions
Tahsin Abrar
Tahsin Abrar

Posted on

Stop Hacking Your .htaccess: A Better Way to Switch Laravel Versions

We’ve all been there. It’s 10:00 PM, you just finished a major update for your Laravel site, and it’s time to go live.

In the past, I used to do what many of us try first: The .htaccess Trick. I would upload the new version to a subfolder and try to "point" the website to it using some clever Apache rules.

It felt smart at the time. But then came the bugs. Broken CSS paths, session errors, and the scariest part accidentally making my .env file public.

Today, I want to show you a much better, "pro" way to do this. It’s called a Symbolic Link (Symlink). It sounds technical, but it’s actually as simple as creating a shortcut on your desktop.

The "Bakery" Analogy

Imagine you run a bakery. You have a display case (your website) where you show your cakes.

  • The .htaccess way: You try to move the entire display case every time you bake a new cake. It’s heavy, slow, and you might drop something.
  • The Symlink way: You keep the display case where it is. You just swap the tray inside. The customers always look at the same spot, but the cake is fresh.

Why the .htaccess method is risky

Before we dive into the "how," here is why you should avoid switching versions via .htaccess:

  1. Security: You risk exposing your sensitive files (like .env) if the redirect isn't perfect.
  2. Path Issues: Laravel functions like public_path() can get confused when the project is buried in subfolders.
  3. Downtime: If you make a typo in the .htaccess file, your whole site goes down with a "500 Internal Server Error."

The Better Way: The "Current" Folder Strategy

Instead of moving files around, we are going to create a structure like this on your server:

/my-project/
├── releases/
│   ├── v1_old/   (Your old code)
│   └── v2_new/   (Your new code)
└── current/      (The "Magic" Shortcut)

Enter fullscreen mode Exit fullscreen mode

Your web server (Apache or Nginx) will always look at the current/public folder. To switch versions, we just tell the current shortcut to point to a different folder.

Step-by-Step Guide

1. Prepare your folders

Upload your new version into a folder inside releases/. Let's call it v2_new.

2. The Magic Command

Open your terminal (SSH). Instead of copying files, run this command:

ln -sfn /home/username/releases/v2_new/public /home/username/public_html

Enter fullscreen mode Exit fullscreen mode

What does this do? It tells the server: "Hey, whenever someone visits public_html, actually show them the files inside v2_new/public." It happens instantly. No loading bars, no waiting.

3. The "Oops" Button (Rollback)

This is my favorite part. If you realize v2_new has a massive bug, you don't need to panic. You can switch back to the old version in one second:

ln -sfn /home/username/releases/v1_old/public /home/username/public_html

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
tahsin000 profile image
Tahsin Abrar • Edited

If you’re tired of typing long ln -sfn commands every time, here’s a cleaner and safer way.

We’ll create a small shell script so switching versions becomes one simple command.

Folder structure (example)

/home/tom1/
├── old.tom.com/
├── new.tom.com/
└── dev.tom.com   → (symlink, active site)
Enter fullscreen mode Exit fullscreen mode

dev.tom.com is what your web server points to.

🛠 Create switch.sh (root folder)

nano switch.sh
Enter fullscreen mode Exit fullscreen mode

Paste this code 👇

#!/bin/bash

BASE="/home/tom"
SYMLINK="$BASE/dev.tom.com"

case "$1" in
  new)
    ln -sfn "$BASE/new.tom.com" "$SYMLINK"
    echo "Switched to NEW version"
    ;;
  old)
    ln -sfn "$BASE/old.tom.com" "$SYMLINK"
    echo "🔙 Rolled back to OLD version"
    ;;
  *)
    echo "Usage: ./switch.sh {new|old}"
    exit 1
    ;;
esac

echo "Current active version:"
ls -l "$SYMLINK"
Enter fullscreen mode Exit fullscreen mode

Make it executable (one time)

chmod +x switch.sh
Enter fullscreen mode Exit fullscreen mode

🚀 How to use (Instant switch)

# Switch to new version
./switch.sh new
Enter fullscreen mode Exit fullscreen mode
# Rollback to old version
./switch.sh old
Enter fullscreen mode Exit fullscreen mode