Call or email today for a free consultation.
Use htaccess to rewrite URLs with query strings
- Mobile-responsive tables March 19, 2014 at 2:39 pm
- Contenteditable text editor in a web browser October 22, 2013 at 5:55 pm
- HTML CSS3 3D Panorama viewer with accelerometer and compass in the stock October 22, 2013 at 12:35 pm
- Circular rotation in Javascript October 22, 2013 at 10:13 am
- How to stroke text in Illustrator: A tutorial July 21, 2013 at 11:20 pm
- How I saved a client $60,000 (or, Where To Tap) July 21, 2013 at 9:21 pm
- How much does a website cost? July 21, 2013 at 8:21 pm
- Use htaccess to rewrite URLs with query strings July 21, 2013 at 7:22 pm
- Ian Tregillis web site July 21, 2013 at 3:04 pm
- Web design as an art form July 21, 2013 at 2:59 pm
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