Add Ending (i.e. Trailing) Slashes to Web Addresses Using a Regular Expression

Trailing Slashes are the very last slash (i.e. “/”) at the end of a URI.


Key Tips for Redirecting with Regular Expressions (i.e. Regex):

Regular expressions are wonderfully powerful, flexible, and extremely dangerous. We use regular expressions when you need to watch for a pattern of characters, words, or numbers. I am in love with them when it comes to Google Sheets or Google Data Studio when you need to remove URL parameters or standardize text dimensions so that things sum as intended.

In redirections, for example, regular expressions can replace a word or redirect a set of pages or redirect all the pages in a category to a new category. If you have 20 pages in that category, you don’t have to set up redirects for each one. You can use a regex to properly 301 redirect them all to their respective new pages (as long as the rest of the URI is consistent, of course).

However, keep in mind, when setting up redirects you can shut down your site, keep images from loading, or create a ton of 404 errors you would never know about until it’s too late. But when done correctly, they can make your world much easier.

Read John Godley’s great post on using regex in his Redirection plugin. He has great tips on avoiding loops and other issues.

I’m writing this post because I could not find any articles on the web about how we can use regex to add a trailing slash for WordPress sites. There are plenty of posts for situations when working with data, using JavaScript or other programming language, HTAccess files, and many other situations. But finding one solution for all URI situations was almost not possible. Here’s how I did it…after much trial and error.

I use the Redirection plugin for my WordPress sites (please donate, it’s an amazing plugin). I’ll use it’s terminology, but that should translate for your other situations. In that context, like an HTAccess file, redirects are processed from the top to the bottom. When anyone visits a page, that web address is matched to redirects one at a time. As soon as one of them applies, it processes it and does not continue through the list. If not ordered properly, you could have a regex applying you don’t intend to, create a loop effect of redirects (and shutting down your site), or creating a very long redirect chain (where redirects happen five or more times in sequence) that Google won’t follow.

The Regex:

Source Regex: ^/([^?.]+)([^/])(\?.*)?$

Target: /$1$2/$3

Source Regex Explanation:

^/Requires a slash to be at the beginning of a URI. Since slashes can be anywhere in a URI, this helps set the stage for whatever is next.
()Parenthesis stores the match so we can pass it along to the “target.”
[^?.]+[^x] – The combo of bracket and hat means that whatever follows the hat should NOT be matched. The whole bracket portion represents ONE character, not a word. In other words, it matches all possible characters BUT the ones following the hat. You can have multiple characters like I do here (i.e. “?” and “.”). All are taken literally (i.e. they don’t need to be escaped like other uses) and there is no limit. I use a question mark to keep this portion from storing URL parameters. I also add a period to keep images from having a slash added to their web addresses. That simple issue creates 404 errors. I’m always amazed how such a tiny little character has the power to shut down a site — whether using regex or programming a site.

IMPORTANT: No matter how many characters you add, it’s still evaluating a single character in your pattern. It’s easy to get confused. If you want to exclude a character from anywhere in a URL (like I’m doing with a period), you must add the plus sign after it like I have. That plus sign means that portion is NOT optional and applies to the entire web address — not just a single character.
[^/]Adding this portion AFTER the prior section tells the system to place it after the prior portion. I know, it’s logical. However, when it’s after a plus sign (+) or asterisk (*), this can produce unexpected results. It’s especially true when a URI has more than one slash. It often stops matching after the first slash it comes to. That means it wouldn’t grab the whole URI like we need. So, it works best if followed by a dollar sign ($) or other clear determinant that wouldn’t show anywhere else in the URL (e.g. question mark). This combination is vital for our scenario to make sure that when a slash is at the end of a URI, this redirect should not be applied. Putting this portion in parenthesis allows you to pass the matched character onto the target URI.
(\?.*)?This portion captures URL parameters (commonly used for campaign tracking or user click tracking in Google Adwords, display ad links, cross domain tracking, and many others. We want those parameters passed to the final destination to keep the marketing source reported in Google Analytics. (Tangent: This may change, but when using regex in the Redirection plugin, the “ignore slash” setting doesn’t apply even if checked.) In regex, the question mark (“?”) usually means the character before it is optional. In our case, we need it to be literal and “escaped.” And the preceding backslash does just that. It means to treat a question mark like a question mark character. But you’ll see at the end of the parenthesis, the question mark is used for pattern intelligence. It means a URI doesn’t have to have the portion before it. Question marks usually just apply to the character just prior to it. But when you put a group of characters in parenthesis — whether you plan to pass the match on or not, it still groups them together so a question mark makes the whole set optional. In our situation here, this combination effectively means this regex applies to regular URIs AND those with parameters. Next, the period & asterisk (.*) essentially means match all characters or none. Whatever if after the question mark, whether something is there or not, it’ll be stored and passed to the target.
$Lastly, this means that whatever is before it must be at the end of the URI. If you didn’t have it, then whatever is before it would match any portion of the URI. In our case, we really need it after the “[^/]” so that the ending slash is identified. But since we sometimes have URL parameters, we need to insert that between the two.

Target Regex Explanation:

/$1To keep things clear and make sure the right parts of a URI are passed on, we didn’t store the first slash of a URI. So we add it back here. $1 is the first matched set of parenthesis in the regex. Based on how we’ve built it here, we’ve captured all of the URI up to either the last character or question mark as long as the URI doesn’t end with a slash.
$2/This portion is the ending character match — as long as it’s not a slash. The regex set prior to this doesn’t capture the last character since we have the exception followed by a dollar sign. We then add the ending slash we want the URIs to have.
$3Lastly, we add back in the URL tracking parameters — if there is a question mark in the URI. There won’t be any error if parameters don’t exist. It’ll just be blank. Easy peasy.

Please add comments if you think I have something wrong or a URI case that will create an error if this is applied. This is all very technical and it’s easy to miss use cases.

I hope this helps. Always glad to share and keep others from spending hours reinventing the wheel!

Regular Expression (Regex) for Phone Numbers – International and U.S. Formats

I have been looking all over the web for a regular expression to use in JavaScript functions to help with formatting phone number links and printing phone numbers on pages for iPhones, Android, and other mobile devices. This process would naturally affect traditional internet browsers for traditional computers but I went down this path mostly for tracking phone number touch events in Google Analytics on only mobile devices. I finally had the epiphany today that created one expression that covered most international and United States phone number formats.

International and U.S. Phone Number Regular Expression

The red areas, represent the numbered portions. Areas in gray represent the options for spaces, dashes, periods, or sometimes nothing between numbers (e.g. the question marks).

Continue reading “Regular Expression (Regex) for Phone Numbers – International and U.S. Formats”

Why WordPress Main Menu Navigation is Missing from Categories

Often I am converting old WordPress sites or working with custom page or post types. When you try to force something other than posts to show in category pages, the main navigation menus fail to display. The core WordPress wp_nav_menu() function doesn’t know what to do in those cases and returns nothing.

Code Solution:

if(is_category()) {
$wp_query = NULL;
$wp_query = new WP_Query(array('post_type' => 'any'));  }
wp_nav_menu( $defaults );
if($thiscat_id!="") { $wp_query=$backup; }

Continue reading “Why WordPress Main Menu Navigation is Missing from Categories”

Excel Tidbits: Limiting Meta Descriptions Characters and Words Like Google

I found it hard to find this information on the web, so after a little bit of work I created an Excel formula that mimics the cut off point Google may naturally use when displaying your page’s Meta description in search engine results pages (SERPs). This can come in handy when you are optimizing your site for search engines (SEO). A well written page description is very helpful in improving a site’s click through rate. Depending on your page’s subject and target audience, writing a description in which the page’s story is incomplete or leaves a person hanging improves the searcher’s interest in clicking through. As a result, the last few words of a description could play a large part in creating that situation. But if Google cuts it off too soon or too late, it may be just enough to lose a person’s interest. Continue reading “Excel Tidbits: Limiting Meta Descriptions Characters and Words Like Google”

Excel Tidbits: Creating Column Header Rows for Sorting

Apple/Mac MS Excel Column Header Selection Using Filters

Well, sad to say this one has stumped me for a long time. I work in Numbers from Apple’s iWorks Suite about as much as I do in Excel. Data sets above about 1000 rows, assuming use of processor-heavy formulas like vlookups, really slow Numbers down. From a visual and formula perspective, Numbers is much easier to use. One of my favorite aspects is the ability to create header rows so that the last row becomes the column headers and the references in formulas. That makes modifying data quicker and improves one’s ability to keep track of a greater variety of segments. Continue reading “Excel Tidbits: Creating Column Header Rows for Sorting”

Excel Tidbits: Extracting Domains from URLs Function

Excel LogoiWorks Numbers LogoI often mash and extract data using Excel. I needed a way to pull out the domain from a web address and compare it with domains from other sheets and activities. This then gives me insight into how often we do work on websites across all our projects.

Version Note: The formula below only works in versions of Excel that can read the xlsx format. Older versions of Excel (pre-2004) are limited to seven nested formulas. This equation has eight. The iserror formulas in the if statements checks for errors and displays whatever is in the A6 cell if an error occurs. If the cell is blank, it should stay blank. I simply used the blank check upfront to make the formula more efficient in processing. Continue reading “Excel Tidbits: Extracting Domains from URLs Function”

Behavioral Ad Targeting Debate

An article by eMarketer spread word on a new study saying people don’t want targeted ads. Earlier studies say differently. How conductors educate the participants within and around questions is just as important as the actual question itself. Context can easily change meaning. Even the order of questions affects respondent perception and subsequent answers. Issues in this conclusion (i.e. data security and customization) need to be approached separately at first and combined down the questionnaire so that customers prioritize their opinion aptly and fears controlled.

My opinion remains unchanged. Customers dislike loosing control of their personal information, yet appreciate customization services. If we communicate our responsibility to protect their information, they will appreciate the benefits we serve and their purchases and activity will speak that message. Continue reading “Behavioral Ad Targeting Debate”

Importing Audio Books into Your iPod

Okay, so this isn’t web related, but it’s important enough to put it out there.

Working for a book publisher and owning an iPod and liking audio books means I want audio books on my iPod. Thomas Nelson has many audio book products (fictionnonfictionbiblesgift booksSpanish). If I don’t buy audio books off iTunes or Audible, then I have to import them from MP3 CDs or audio CDs. The problem with iTunes is that Apple didn’t really consider the process of importing audio books. Instead, iTunes treats any audio file like a music file unless it has an M4B extension (as of iTunes 7.6 and earlier versions). M4B is Apple’s audio book file format.

What You Really Need to Know: If you change the extension of any AAC file (mp4 or m4a) to M4B and add the files to your iTunes library, it automatically applies its audio book features to the file. There is no special encoding to the file needed. Continue reading “Importing Audio Books into Your iPod”

We Are Media

I cannot authoritatively speak for all of us, but from what I have seen, we in the print publication industries find it hard to include digital products in our business. But I think it’d be no surprise to all of us that if we don’t consider publishing more digital products in the near future, our business will be on track for exinction.

But I want to take the concept farther. If we don’t intricately integrate digital publishing into our publishing process and product concepts, we will eventually fail in the dust of other publishers who do. And I’m not looking 10 years down the line as we may naturally feel it is. It’s much, much closer.

I’d like to give some basics to be sure we are on the same page. When I speak about digital publishing, I’m not talking about creating a book or magazine in Adobe InDesign or Quark and then having it printed out. I’m talking about two general concepts. Continue reading “We Are Media”