Upgrade your web design
3232Design is a Minneapolis web design studio offering affordable high-quality communication design specializing in websites and interactive media; also producing and integrating business systems, print marketing, online marketing and brand identity design for creative professionals and small to medium-sized businesses.

Call or email today for a free consultation.

Use htaccess to rewrite URLs with query strings

show all entries...

I recently had to update my site from ColdFusion URLs with a query string to search engine-friendly URLs. A lot of those blog entries were being hit every day, so I wanted to port them over and rewrite the URL as a 301 redirect so Google results would still point to the right place. What I discovered is that your friendly neighborhood htaccess file is up there with regex as some of the most misunderstood arcana out there.


I sacrificed a few chickens, and here is what I learned.

When rewriting a URL, you can do some fairly simple things to make sure that your old URL redirects to the new place without any hassle at all. Old links will still work, and users can still get to see the new pretty URL. This usually looks something like this:

RewriteEngine On
RewriteBase /

RewriteRule ^/web/Ian\ Tregillis/Web\ Site$ http://3232design.com/home/Ian-Tregillis-web-site [R=301,L]


The first two lines tell the rewriter to turn on for these files, then use '/' as the base for rewriting the URLs. The next line is the actual rewriting line. The '^' symbol represents the start of a line, while the '$' represents the end of a line. A backslash ('\') tells the next character to be that literal character rather than what it represents, in this case a space. A space in the rule means that you're done with that part of the rule, and if there is a space in the URL then we're hosed unless we escape it with a backslash.

So the first part of the rule is what's coming in, the URL that the user followed here. Then a space, then the fully-qualified URL that we would like to redirect that URL to. After another space, some little codes to tell the browser (and search engines) how to handle the link. In this case, the 'R=301' tells it to redirect permanently to the new URL. The 'L' tells it to stop processing the rule set.

That's cool for redirecting pages, but this doesn't work if you've got a query string. In my case, it was /blog.cfm?id=123 and the 123 was the crucial part that distinguished between all of the different blog articles. Since the rewrite engine was just appending those and not processing them, I had to dig deeper. What I came up with was adding this:

RewriteCond   %{QUERY_STRING}   ^id=60$
RewriteRule   ^blog.cfm$ http://3232design.com/home/How-to-stroke-text-in-Illustrator-A-tutorial? [R=301,L]

The first line adds a condition to our ruleset. These are just added until an 'L' shows up. This condition targets any file with a query string that contains 'id=60' only. Once we've narrowed the condition down to a specific query string, the next line actually rewrites the page, based on a file name of 'blog.cfm' (so now we've got 'blog.cfm?id=60' only) and rewrites it to my new, pretty, search engine-friendly URL. The question mark at the end of my new URL tells the engine not to add the previous query string to the new URL. Without the question mark, the new url would be  /home/How-to-stroke-text-in-Illustrator-A-tutorial?id=60 which, while not a big deal, is irritating and not useful. Again, the 301 means a permanent redirect (a 302 would be temporary) and the 'L' tells it to stop processing this rule and ready the engine for an entirely new rule.

That's it, really. It took me forever to find the magic combination that would allow me to write a new URL based on the old URL's query string, but there it is. You could make it fancier by adding wildcards if you wanted, but my case didn't require it. 

Now you know! And you didn't have to sacrifice any chickens to find out, either.

You're welcome.

Comments

No comments yet.