Script Python yang kamu tulis sudah benar dan bisa dijalankan, tapi ada beberapa hal yang perlu diperhatikan agar hasilnya bisa langsung dipakai:


1️⃣ Simpan file Python

  1. Buka text editor (VSCode, Notepad++, atau nano di Linux).

  2. Simpan script dengan ekstensi .py, misal:

    create_redirects.py
  3. Pastikan Python sudah terinstall di komputer/server kamu.


2️⃣ Jalankan script

Di terminal/command prompt:

python create_redirects.py

3️⃣ Pasang .htaccess ke tiap domain


4️⃣ Tips tambahan


Kalau mau, saya bisa buatkan versi Python yang otomatis upload semua .htaccess ke 100 domain via FTP/SFTP, jadi tidak perlu manual upload satu per satu.

Jika Kalau PAKAI NGINX Berbeda! Ini Caranya

https://i.imgur.com/H0gJkn0.png

Kalau pakai Nginx, cara kerjanya berbeda karena Nginx tidak pakai .htaccess. Semua redirect harus didefinisikan di file konfigurasi server. Kamu bisa pakai script Python untuk generate konfigurasi otomatis.


1️⃣ Contoh Python Script untuk Nginx

domains = [
"feuerloescher.biz",
"evn.biz",
"fdo.biz",
"KennedyLaw.biz",
"grupposalteco.biz",
"tochicers.biz",
"trndly.biz",
"dvrenejf.biz",
"iji.biz",
"iui.biz",
"nenga-photo.biz",
"BudBrothers.biz",
"ClickBunker.biz",
"iay.biz",
"Enhancements.biz",
"SupraCom.biz",
"vut.biz",
"bcu.biz",
"eph.biz",
"MerdekaPoker.biz",
"rno.biz",
"MooRecreative.biz",
"FirstPenguin.biz",
"amyatwood.biz",
"The-Wing.biz"
]
target = “https://maharajadaytona.com”nginx_template = “””
server {{
server_name {domain} www.{domain};
return 301 {target}$request_uri;
}}
“””
for domain in domains:
filename = f”./{domain}_redirect.conf”
with open(filename, “w”) as f:
f.write(nginx_template.format(domain=domain, target=target))
print(f”Config Nginx untuk {domain} dibuat: {filename}“)


2️⃣ Cara pakai

  1. Script di atas akan membuat 1 file .conf per domain.
    Contoh output:

    feuerloescher.biz_redirect.conf
    evn.biz_redirect.conf
  2. Upload atau pindahkan file ke folder Nginx:

    /etc/nginx/sites-available/
  3. Buat symbolic link ke sites-enabled:

    sudo ln -s /etc/nginx/sites-available/feuerloescher.biz_redirect.conf /etc/nginx/sites-enabled/
  4. Reload Nginx agar konfigurasi aktif:

    sudo nginx -t # cek konfigurasi
    sudo systemctl reload nginx

3️⃣ Catatan


Kalau mau, saya bisa buatkan versi Python all-in-one yang langsung generate dan deploy konfigurasi Nginx untuk semua domain sekaligus, termasuk reload otomatis.

Contoh Script Redirect Sekaligus :

Siap! Berikut versi Python Nginx redirect all-in-one yang ambil list domain dari file .txt, jadi kamu bisa langsung pakai ratusan domain tanpa edit script.


1️⃣ Siapkan file domain

Buat file misalnya domains.txt:

feuerloescher.biz
evn.biz
fdo.biz
KennedyLaw.biz
grupposalteco.biz
tochicers.biz
trndly.biz
dvrenejf.biz
iji.biz
iui.biz
nenga-photo.biz
BudBrothers.biz
ClickBunker.biz
iay.biz
Enhancements.biz
SupraCom.biz
vut.biz
bcu.biz
eph.biz
MerdekaPoker.biz
rno.biz
MooRecreative.biz
FirstPenguin.biz
amyatwood.biz
The-Wing.biz

2️⃣ Script Python

#!/usr/bin/env python3
import os
import subprocess
# ===================== CONFIG =====================
domain_file = “domains.txt” # File txt berisi list domain
target = “https://maharajadaytona.com”nginx_available = “/etc/nginx/sites-available”
nginx_enabled = “/etc/nginx/sites-enabled”nginx_template = “””
server {{
server_name {domain} www.{domain};
return 301 {target}$request_uri;
}}
“””

# ===================== SCRIPT =====================
def read_domains(file_path):
if not os.path.exists(file_path):
print(f”[ERROR] Domain file not found: {file_path}“)
return []
with open(file_path, “r”) as f:
domains = [line.strip() for line in f if line.strip()]
return domains

def create_nginx_conf(domain):
conf_path = os.path.join(nginx_available, f”{domain}_redirect.conf”)
with open(conf_path, “w”) as f:
f.write(nginx_template.format(domain=domain, target=target))
print(f”[OK] Config created: {conf_path}“)
return conf_path

def enable_site(conf_path):
link_path = os.path.join(nginx_enabled, os.path.basename(conf_path))
if not os.path.exists(link_path):
os.symlink(conf_path, link_path)
print(f”[OK] Enabled site: {link_path}“)
else:
print(f”[SKIP] Site already enabled: {link_path}“)

def reload_nginx():
try:
subprocess.run([“nginx”, “-t”], check=True)
subprocess.run([“systemctl”, “reload”, “nginx”], check=True)
print(“[OK] Nginx reloaded successfully!”)
except subprocess.CalledProcessError:
print(“[ERROR] Nginx reload failed! Check config syntax.”)

def main():
domains = read_domains(domain_file)
if not domains:
print(“[ERROR] No domains to process.”)
return
for domain in domains:
conf_path = create_nginx_conf(domain)
enable_site(conf_path)
reload_nginx()

if __name__ == “__main__”:
main()


3️⃣ Cara Pakai

  1. Simpan script, misal auto_redirect_nginx_from_txt.py.

  2. Simpan domains.txt di folder yang sama.

  3. Jalankan script dengan root/SSH:

sudo python3 auto_redirect_nginx_from_txt.py
  1. Script akan:

    • Membaca semua domain dari file domains.txt.

    • Membuat konfigurasi redirect 301 otomatis untuk setiap domain.

    • Enable site dan reload Nginx.


Sekarang kamu bisa tambahkan ratusan domain di domains.txt tanpa harus edit script lagi.

Kalau mau, saya bisa buatkan versi lebih canggih: domain bisa otomatis dicek apakah DNS sudah pointing ke server sebelum redirect dibuat, supaya aman untuk live server.

CARA 3 UNTUK LEBIH BANYAK DOMAIN MAP FILE

Panduan Lengkap: Redirect Menggunakan Map File di Nginx

A “map file redirect”Β refers to the use of a map file to define relationships between old and new URLs, which is then used by web servers or other systems to perform URL redirections, often for tasks like managing website migrations, fixing broken links, or ensuring canonical hostnames.Β For example, Nginx can use aΒ .mapΒ file to map old URIs to new ones for permanent redirects, while Sitecore uses a “Redirect Map” feature to manage 301, 302, or server transfer redirects.Β 

Key Concepts
  • Map File:Β 

    A simple text file containing a list of mappings, where each line pairs an original URL or pattern with a new URL or value.Β 

  • The process of sending a user from one URL (the old URL) to another (the new URL).Β 

  • HTTP Status Codes:
    • 301 (Permanent Redirect):Β Indicates that the new URL is a permanent replacement for the old URL.Β 
    • 302 (Temporary Redirect):Β Indicates that the redirection is temporary.Β 
    • Server Transfer:Β A type of redirect that keeps the URL the same on the client-side and reduces server requests.Β 
  • Case-Insensitivity:Β 

    URL mappings are often case-insensitive, meaning that the case of the URL path does not affect the match.Β 

How it Works (Examples)
  • Nginx:Β 

    You define aΒ mapΒ directive in your Nginx configuration to include aΒ .mapΒ file.Β Nginx then uses this file to map requested URIs to new URIs, allowing for mass redirects from a simple, external file.Β 

  • You create a “Redirect Map” in the Sitecore interface, which allows you to define mappings between old and new URLs for permanent, temporary, or server-based redirects.Β 

  • The URL Rewrite Module in IIS can use a rewrite map file to handle URL rewriting, providing a way to map incoming URLs to internal representations without needing complex rewrite rules.Β 

When to Use It
  • Website Migrations:Β 

    To redirect users from old pages to new content after a site restructure or platform change.Β 

  • Fixing Broken Links:Β 

    To point users away from invalid URLs to the correct pages.Β 

  • Canonical Hostname Management:Β 

    To force all users to a single, preferred version of your domain (e.g., redirecting fromΒ example.comΒ toΒ www.example.com).Β 

  • Simplifying Complex Rules:
    To manage a large number of redirects in a clean, organized file instead of embedding them directly in server configurations.
    =========================================================================

    Redirect dengan map file adalah cara yang praktis dan terstruktur untuk mengatur banyak redirect di server tanpa harus membuat puluhan atau ratusan konfigurasi terpisah. Teknik ini sangat berguna kalau kamu sedang melakukan migrasi website, memperbaiki broken link, atau mengatur hostname canonical.


    Apa Itu Map File Redirect?

    Map file redirect adalah file teks sederhana yang berisi pasangan URL lama β†’ URL baru. File ini dibaca oleh web server (misalnya Nginx) untuk melakukan redirect secara otomatis.

    Konsep Utama:

    • Map File: Daftar aturan redirect (baris demi baris).

    • URL Redirection: Proses mengarahkan pengunjung dari URL lama ke URL baru.

    • HTTP Status Code:

      • 301 β†’ Permanent Redirect (SEO friendly).

      • 302 β†’ Temporary Redirect (sementara).

    • Server Transfer: Kadang redirect bisa dilakukan tanpa mengganti URL di browser.

    • Case-Insensitive: Biasanya URL dianggap sama meskipun huruf besar/kecil berbeda.


    Kapan Menggunakan Map File?

    • Migrasi Website β†’ Redirect dari struktur lama ke struktur baru.

    • Perbaikan Broken Links β†’ Mengarahkan link mati ke halaman valid.

    • Canonical Hostname β†’ Memaksa semua pengunjung ke versi utama domain (misalnya example.com β†’ www.example.com).

    • Aturan Redirect Banyak β†’ Lebih rapi dibanding nulis satu-satu di server {} Nginx.


    Cara Kerja di Nginx

    1. Buat File Map Redirect

    Buat file baru di server, misalnya /etc/nginx/redirects.map:

    map $host $redirect_target {
    default "";
    example.com https://www.example.com;
    oldsite.com https://newsite.com;
    blog.example.com https://example.com/blog;
    }

    πŸ‘‰ Artinya:

    • Semua request ke example.com diarahkan ke https://www.example.com.

    • Semua request ke oldsite.com diarahkan ke https://newsite.com.

    • Semua request ke blog.example.com diarahkan ke https://example.com/blog.


    2. Edit Konfigurasi Nginx

    Buka file konfigurasi utama, misalnya /etc/nginx/sites-enabled/default:

    http {
    include /etc/nginx/redirects.map;

server {
listen 80 default_server;
server_name _;

location / {
if ($redirect_target != “”) {
return 301 $redirect_target$request_uri;
}
return 444; # drop kalau tidak ada mapping
}
}
}


3. Tes Konfigurasi & Reload Nginx

Jalankan perintah:

sudo nginx -t
sudo systemctl reload nginx

Kalau tidak ada error, redirect langsung aktif.


Contoh Kasus

Migrasi Domain

Dari oldbrand.com ke newbrand.com:

map $host $redirect_target {
default "";
oldbrand.com https://newbrand.com;
}

Redirect Banyak Domain ke Satu Domain

Kalau kamu punya banyak domain alias, cukup masukkan semua:

map $host $redirect_target {
default "";
site1.com https://mainbrand.com;
site2.com https://mainbrand.com;
site3.net https://mainbrand.com;
}

Alternatif di Platform Lain


Kesimpulan

Menggunakan map file redirect di Nginx sangat efisien untuk:

Cukup simpan aturan di satu file .map lalu include di Nginx, kamu bisa kelola redirect skala besar dengan lebih mudah. πŸš€


πŸ‘‰ Mau aku bikinin juga script Python kecil untuk auto-generate file redirects.map dari daftar JSON/CSV? Jadi tinggal input list domain β†’ langsung jadi map file siap pakai.

Oke bre, aku bikinin contoh real biar gampang dipahami πŸ‘‡


πŸ”Ή 1. Contoh File redirects.map

Buat file /etc/nginx/redirects.map:

map $host $redirect_target {
default "";

# Migrasi domain lama ke domain baru
oldbrand.com https://newbrand.com;

# Canonical host (non-www ke www)
example.com https://www.example.com;

# Subdomain diarahkan ke path tertentu
blog.example.com https://www.example.com/blog;

# Banyak domain diarahkan ke 1 domain utama
site1.com https://mainbrand.com;
site2.com https://mainbrand.com;
site3.net https://mainbrand.com;
}


πŸ”Ή 2. Konfigurasi Nginx

Edit file /etc/nginx/sites-enabled/default (atau file domain kamu):

http {
include /etc/nginx/redirects.map;

server {
listen 80 default_server;
server_name _;

location / {
if ($redirect_target != “”) {
return 301 $redirect_target$request_uri;
}
return 444; # kalau domain tidak ada di mapping, diblock
}
}
}


πŸ”Ή 3. Tes & Reload

sudo nginx -t
sudo systemctl reload nginx

Sekarang:


πŸ”Ή 4. Bonus: Auto Generate dari Python

Kalau domain banyak banget, bikin file generate_map.py:

#!/usr/bin/env python3

redirects = {
“oldbrand.com”: “https://newbrand.com”,
“example.com”: “https://www.example.com”,
“blog.example.com”: “https://www.example.com/blog”,
“site1.com”: “https://mainbrand.com”,
“site2.com”: “https://mainbrand.com”,
“site3.net”: “https://mainbrand.com”,
}

with open(“redirects.map”, “w”) as f:
f.write(“map $host $redirect_target {\n”)
f.write(” default \”\”;\n\n”)
for old, new in redirects.items():
f.write(f” {old:<20} {new};\n”)
f.write(“}\n”)

print(“βœ… redirects.map generated”)

Jalankan:

python3 generate_map.py

Output redirects.map langsung siap include di Nginx. πŸš€

Aku jelasin dulu ya bre, karena di sini aku nggak bisa langsung eksekusi Python di server kamu (nggak ada akses ke VPS/Ubuntu kamu dari chat ini).
Tapi aku bisa kasih kamu step-by-step cara jalanin di server Ubuntu yang kamu tunjukin tadi (root@ubuntu-s-2vcpu-4gb-sgp1-01).


πŸ”Ή Step 1: Buat File CSV

Login ke server, bikin file redirects.csv di folder /etc/nginx/:

nano /etc/nginx/redirects.csv

Isi dengan contoh:

old_domain,new_url
oldbrand.com,https://newbrand.com
example.com,https://www.example.com
blog.example.com,https://www.example.com/blog
site1.com,https://mainbrand.com
site2.com,https://mainbrand.com
site3.net,https://mainbrand.com

πŸ”Ή Step 2: Buat Script Python

Bikin file generate_map_from_csv.py:

nano /etc/nginx/generate_map_from_csv.py

Paste script ini:

#!/usr/bin/env python3
import csv
import subprocess

INPUT_FILE = “/etc/nginx/redirects.csv”
OUTPUT_FILE = “/etc/nginx/redirects.map”

def generate_map():
with open(INPUT_FILE, newline=, encoding=“utf-8”) as csvfile:
reader = csv.DictReader(csvfile)
rows = list(reader)

with open(OUTPUT_FILE, “w”, encoding=“utf-8”) as f:
f.write(“map $host $redirect_target {\n”)
f.write(” default \”\”;\n\n”)
for row in rows:
old = row[“old_domain”].strip()
new = row[“new_url”].strip()
f.write(f” {old:<20} {new};\n”)
f.write(“}\n”)

print(f”βœ… Generated {OUTPUT_FILE} with {len(rows)} entries”)

# Test config dan reload otomatis
subprocess.run([“nginx”, “-t”])
subprocess.run([“systemctl”, “reload”, “nginx”])

if __name__ == “__main__”:
generate_map()


πŸ”Ή Step 3: Ubah Permission

Kasih izin eksekusi:

chmod +x /etc/nginx/generate_map_from_csv.py

πŸ”Ή Step 4: Edit nginx.conf

Tambahkan include map file di nginx.conf (atau default.conf):

http {
include /etc/nginx/redirects.map;

server {
listen 80 default_server;
server_name _;

location / {
if ($redirect_target != “”) {
return 301 $redirect_target$request_uri;
}
return 444;
}
}
}


πŸ”Ή Step 5: Generate & Reload

Sekarang jalankan:

python3 /etc/nginx/generate_map_from_csv.py

Kalau sukses, kamu bakal lihat output:

βœ… Generated /etc/nginx/redirects.map with 6 entries
nginx: configuration file /etc/nginx/nginx.conf test is successful

Lalu otomatis reload Nginx.


Jadi tiap kali kamu update redirects.csv, cukup jalankan script itu lagi β†’ file redirects.map diperbarui β†’ Nginx reload otomatis πŸš€

Jika ingin Set Jadwal Tugas Menggunakan Bot Bisa Kali Ini kita pakai Teman Cronjob.

βœ…, cronjob itu dipakai untuk menjadwalkan tugas (job) agar dijalankan otomatis pada waktu tertentu.

Misalnya:


πŸ”Ή Pola Dasar Cronjob

Formatnya:

* * * * * perintah
- - - - -
| | | | |
| | | | └─ Hari dalam minggu (0–7, Minggu = 0 atau 7)
| | | └─── Bulan (1–12)
| | └───── Tanggal (1–31)
| └─────── Jam (0–23)
└───────── Menit (0–59)

πŸ”Ή Contoh Cronjob

  1. Jalanin script tiap jam:

0 * * * * python3 /etc/nginx/generate_map_from_csv.py
  1. Jalanin script tiap 30 menit:

*/30 * * * * python3 /etc/nginx/generate_map_from_csv.py
  1. Jalanin tiap hari jam 3 pagi:

0 3 * * * python3 /etc/nginx/generate_map_from_csv.py
  1. Jalanin tiap Senin jam 7 malam:

0 19 * * 1 python3 /etc/nginx/generate_map_from_csv.py

πŸ”Ή Cara Setting Cronjob

  1. Buka editor cron:

crontab -e
  1. Tambahkan baris sesuai kebutuhan (contoh tiap jam):

0 * * * * python3 /etc/nginx/generate_map_from_csv.py
  1. Simpan, cron akan otomatis aktif.


⚑ Jadi bener kata kamu: cronjob fungsinya untuk menentukan waktu kapan perintah/script dijalankan secara otomatis.