Question

NGINX rewrite script

Hi, Sometimes I’ve to add hundreds of redirects to my NGINX server block. Such a script could definitely make my work easier. I didn’t find ready-made solutions. I’m writing a script that gets addresses from a file and creates rewrite rules for NGINX based on it. The file just contains the old address and the new address separated by a space, for example:

old.html new.html
oldpage newpage
error.php 404

So I should get something like this:

rewrite ^/old.html$ https://domain.com/new.html permanent;
rewrite ^/oldpage$ https://domain.com/newpage permanent;
rewrite ^/error.php$ https://domain.com/404 permanent;

For now, my script looks like this:

if [ "$#" -eq 0 ]; then
    echo "Usage: ./rewrite.sh <domain> <file>"
    exit
else
    DOMAIN=${1}
    FILE=${2}
fi

sed -e 's/$/ permanent;/' -i $FILE #end of each line

It does nothing but add “permanent ;” to the end of each line. How do I add “rewrite ^/” to the beginning of each line? I tried this way, but it doesn’t work :

REW=“rewrite ^/” sed -i -e “s/^/‘${REW}’/” $FILE

I also have no idea how to add “$ https://domain.com/” in the beginning of the new address.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
December 19, 2021
Accepted Answer

Hi there,

There are probably multiple ways to do it, but I would personally use a for loop to loop through each of the lines fo the file and then use printf to construct the redirect rule as follows:

#!/bin/bash

if [ "$#" -eq 0 ]; then
    echo "Usage: ./rewrite.sh <domain> <file>"
    exit
else
    DOMAIN=${1}
    FILE=${2}
fi

OLDIFS="$IFS"
IFS=$'\n' # bash specific
for line in $(cat ${FILE})
do
    old=$(echo $line | awk '{print $1}')
    new=$(echo $line | awk '{print $2}')
    printf "rewrite ^/%s$ https://${DOMAIN}/%s permanent;\n" "$old" "$new"
done

IFS="$OLDIFS"

Quick rundown:

  • It sets the IFS (Internal field separator) variable as separators so that the for loop would go through each separate line
  • Then we start the for loop
  • Using awk we get the old and the new URLs as we follow the conversion that the first one is the old URL and the second is the new one
  • Finally, we use the printf to construct the rules

This will print the results on the screen and you can copy them from there. Or alternatively, when running the script you could send the STDOUT to a file: bash script.sh domain.com file.txt > new_rules.txt.

Hope that this helps! Best, Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.