Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Module of the Day: Path Redirect Import

Today we're going take a look at the Path Redirect Import module, which lets you import redirects in bulk into your Drupal site.

All of the modules described in this post are available on Stanford Sites.

What Are Redirects, and Why Do I Care?

A redirect is when you take a user from one URL (or path) on your website and send him to another URL, without any action on his part. If you learned HTML back in the good ol' days, you may be familiar with the following code:

">http://example.com/">
[/geshifilter-code]

That's what's known as a "Meta refresh", and it's how things were done back in the day. (Unfortunately, meta refreshes are slow and janky, and there are better ways to do them now.)

There are several reasons why you might want to put a redirect(s) in place:

  • You have renamed a page, or updated its path
  • You have restructured your website
  • You have completely rebuilt your website (e.g., moved from static HTML to a Content Management System, such as Drupal) and you want to redirect pages that end in ".html" to their semantic equivalents

Stanford users may be familiar with redirects in the context of the "WWW to Web" migration from Summer, 2014.

One Redirect

The Redirect module in Drupal allows you to create redirects one at a time.

  1. Go to admin/config/search/redirect/add
  2. Enter the source path in the From field
  3. Enter the destination path in the To field
    • The destination can be a path, if it's in your Drupal site, or a full URL if you want to redirect to an external website (cf. "Rickroll")
  4. Almost all the time, you will want to leave Redirect status set to "Default (301)". You can learn more about HTTP status codes at HTTP Status Dogs.

Screenshot of adding redirects, described above

Many Redirects

"Great, but what if I want to create 100 redirects?"

Enter the Path Redirect Import module. It allows you to create a .csv file of multiple redirects, and import them in a batch.

First, be sure you have enabled the module; it is labeled as "Redirect Import" at admin/modules, and it is not enabled by default on Stanford Sites.

Next:

  1. Create a spreadsheet with the following columns: old_url, new_url, redirect_code, language
  2. Populate that spreadsheet with all of the values that you would put in the From and To fields when creating redirects one at a time
    • redirect_code should be "301"
    • language should be "en"
    • Remember that the From paths are relative to your Drupal install; in the example below, we are presuming that we are moving from a static HTML website to Drupal, therefore we want to redirect users who have bookmarked "foo.stanford.edu/contact/index.html" to "foo.stanford.edu/about/contact-and-location".
  3. Save the spreadsheet as a "Comma Separated Values (.csv)". If you are using MS Excel on a Mac, save as "Windows Comma Separated (.csv)"
  4. Go to admin/config/search/redirect/import
  5. Upload your .csv file by clicking the "Choose File" button
  6. Leave the other form fields as their defaults
  7. Click the "Import" button

Your redirects will be imported, and you will see a message describing how many redirects were created, and how many failed.

You can download a sample .csv file here.

Screenshot of comma separated values file, described above

Path Redirect Import screen, described above

Technical Details and More Information

  • Be sure to fill out the "redirect_code" and "language" columns, even if you just want to accept the defaults. See "Language column is required in .csv" for more details.
  • The Redirect module implements the redirection in hook_init(), and adds about 400ms to the page load time to perform the redirect. Therefore if you are able to edit your site's .htaccess file, and have a limited number of redirects, it is probably better to implement the redirects at the .htaccess level.
  • The Redirect module cannot do wildcard redirects. See "Support wildcards in path matching" for more details.
  • Path Redirect Import implements Drupal's Batch API, so the number of redirects that you can import is theoretically unlimited. I have not tried beyond 100 or so, however.

Categories: 

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web