Skip to content

301 Redirects

Extract Existing URLs from Magento

This can be done using magerun:

docs

magerun sys:url:list --add-all <store ID> '{path}' > paths.txt

Test Existing URLs Against Development Site

BASH

This can be used to check which paths are working while the site is still behind basic auth. For testing after the site has gone live see the Google Docs option below.

#!/usr/bin/env bash
readonly DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
cd $DIR;
set -e
set -u
set -o pipefail
standardIFS="$IFS"
IFS=$'\n\t'
echo "
===========================================
$(hostname) $0 $@
===========================================
"

baseUrl='';
pathsFile='paths.txt';
outputFile='301_redirect_test_output.txt';

# Basic Auth
user='';
pass='';

if [ -f $outputFile ]
then
    rm $outputFile;
fi

readarray paths < $pathsFile;
for path in ${paths[@]}
do
    url="${baseUrl}${path}";
    httpCode=$(curl -s -o /dev/null -u "$user:$pass" -w "%{http_code}" "${url}");
    if (( $httpCode != 200 )) && (( $httpCode != 301 ))
    then
        echo "$httpCode, ${url}" >> $outputFile;
    fi
done;

Google Docs

Note

This only works for sites which aren't behind basic auth.

You can use the following Google Docs script to check the status of your pages. This was take from here.

Go to Tools » Script Editor » Name Your Script Editor » OK » Add Below Code.

function HTTPResponse( uri )
{
  var response_code ;
  try {
     response_code = UrlFetchApp.fetch( uri ).getResponseCode().toString();
  }
  catch( error ) {
     response_code = error.toString().match( / returned code (\d\d\d)\./ )[1];
  }
  finally {
     return response_code ;
  }
}

You can easily get this by copying this spreadsheet.

Implement Redirects

NGINX map

Note

This doesn't seem to work with Plesk. See NGINX rewrites below

You can use an NGINX map to efficiently map your old paths to new ones using the following:

# includes/301_redirects.conf

map $uri $redirect {
    /old/path /new/path;
    /another/old/path /another/new/path;
    ...
}

This then needs to be included ideally within nginx.conf.

You make use of the map in your vhost config using:

if ($redirect) {
    rewrite ^ $redirect permanent;
}

Redacted

You can see an example of this in IPM's server config.

NGINX rewrites

If you aren't able to use a map you can simply add a large number of rewrites to your vhosts config.

rewrite ^/old/path$ /new/path permanent;
rewrite ^/another/old/path$ /another/new/path permanent;
...

This might be your only option with Plesk.

Note

In order to be able to add this in Plesk you need to ask for access to the admin account. You add your rewrites to Subscriptions > [your subscription] > Apache & Nginx Settings > Additional nginx directives