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:
-
Security: You risk exposing your sensitive files (like
.env) if the redirect isn't perfect. -
Path Issues: Laravel functions like
public_path()can get confused when the project is buried in subfolders. -
Downtime: If you make a typo in the
.htaccessfile, 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)
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
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
Top comments (1)
If you’re tired of typing long
ln -sfncommands 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)
dev.tom.comis what your web server points to.🛠 Create
switch.sh(root folder)Paste this code 👇
Make it executable (one time)
🚀 How to use (Instant switch)