Digital Marketing Blog

//Alien 301 Redirects: URLs with Query Strings

Alien 301 Redirects: URLs with Query Strings

By |2018-05-04T07:10:02+00:00October 25th, 2017|Categories: Technical SEO|0 Comments

alien with mod rewrite quote

How to do 301 Redirects for URLs with Query Strings Using .htaccess

My personal opinion is that it was actually aliens that wrote all of the Apache mod_rewrite reference documentation. I mean, really, that stuff makes my head implode and explode simultaneously. Using that reference material to figure out how to solve the 301 redirect issue I was having was completely pointless. Luckily, I was able to piece together some ideas from hanging out on various forums. I hope the following will be helpful to you, if you are experiencing something similar.

Old Joomla URLs

Here was the first problem I encountered: I had a client migrating from Joomla to WordPress. Now, I’ve done plenty of simple 301 redirects using htacess or even using WordPress plugins such as redirection. So, when it came time to implement redirects from the old Joomla URLs to the new WordPress posts and pages, that was the first place I turned. No dice. At first, I didn’t even know why it wasn’t working.

Here is what I was dealing with. The old Joomla URLs contained the addition of a [cm_tooltip_parse]query string[/cm_tooltip_parse] like this:

http://www.mywebsite.com/?option=com_content&task=view&id=44&Itemid=45

File Path = http://www.mywebsite.com/
Separator = ?
Query String = option=com_content&task=view&id=44&Itemid=45

Traditional redirect methods would not work because of that query string. After several failed attempts, this is the solution that finally worked:

RewriteCond %{REQUEST_URI} /index\.php
RewriteCond %{QUERY_STRING} option=com_content&task=view&id=44&Itemid=45
RewriteRule ^(.*)$ http://mywebsite.com/about-us/? [L,R=301]

For those interested in knowing what is actually happening with each line, here is the deal:

  1. Rewrite condition number one: Anytime this URL (/index.php) is requested…
  2. Rewrite condition number two: And if it contains this query string (option=com_content&task=view&id=44&Itemid=45)
  3. Use this rewrite rule: Permanently redirect (301) the page to this page: (http://mywebsite.com/about-us/)

“URI” stands for “Uniform Resource Identifier,” and is the portion of a web page’s URL following the domain name. For example, the URI for www.mywebsite.com/about.html is /about.html.

Here is what some of the characters mean:

% = Indicates that this is an Apache variable
^ = This is an anchor
$ = This is also an anchor
? = Non-Greedy Quantifier (in this case basically telling the Regex engine that there is zero or one of the preceding element)
. = Match any character
* = Match zero or more of the preceding
\ = Escape character (like the one found in ‘…index.\php’) Because a period has its own meaning within Regex syntax, it has to be “escaped” so that it can be treated as simply a dot within the directory or file listed (in this case, ‘…index.php’).

For more, check out this cheat: sheet: http://www.cheatography.com/davechild/cheat-sheets/mod-rewrite/

Another Example: Old Mura CMS URLs

I ran into another example that threw me for a loop. I was trying to redirect these URLs from a site using Mura CMS and kept getting an error. Needed to redirect a URL like this:

http://www.mywebsite.com/blogs/dsp_display_blog.cfm?blog_id=15

To this new one: http://www.mywebsite.com/blog/

The problem is that I assumed I need to include ‘…/blogs/…’ in the RewriteCond line. It turned out I only needed the part of the file path that was attached to the query string. Here is what finally worked:

RewriteCond %{REQUEST_URI} /dsp_display_blog\.cfm
RewriteCond %{QUERY_STRING} blog_id=15
RewriteRule ^(.*)$ http://www.mywebsite.com/blog/? [L,R=301]

Hopefully this was helpful and eased some frustration. If you are still having trouble with redirecting URLs with query strings, drop me a line; I can try to help.

And remember, one day your website will become a host for an intergalactic message from whatever aliens came up with this stuff.

For more mod_rewrite goodness visit: Apache mod_rewrite Introduction