Beantin webbkommunikation

Beantin Beta Blog

Experiments, investigations, and tests of web things
October 12, 2010

Generating automatic QR codes with YOURLS

When Goo.gl launched a few weeks ago, they included a feature where adding .qr to the end of a Goo.gl link produced a QR Code for the link. Bit.ly have started doing the same, using the same .qr extention. The idea being you can quickly generate a code, or transfer a link to your mobile device.

So this page for example is http://goo.gl/fBx a with Google’s shortener, and http://goo.gl/fBxa.qr as a QR code. Similarly http://bit.ly/aKAC4j with Bit.ly and http://bit.ly/aKAC4j.qr as a 2d barcode.

YOURLS

I run my own hosted URL shortener using YOURLS on my t1n.se domain. It doesn’t natively support the generation of QR code, but there was a discussion earlier in the year. The main result of that was an awkward “plug-in” which in my eyes over complicates the issue.

In reality it’s very straight forward. http://t1n.se/gaqrc is my shortener link to this page, and http://t1n.se/gaqrc.qr produces a QR code

How?

Just add the following line to the .htaccess file in your YOURLS installation directory. Add it as the first RewriteRule (ie before any YOURLS ReWriteRules).

RewriteRule ^([0-9A-Za-z]+).qr/?$ http://chart.apis.google.com/chart?
cht=qr&chs=200x200&chl=http://%{HTTP_HOST}/$1 [L]

Then that’s it. Job done!

It’s worth noting that this tip isn’t limited to YOURLS, it will work for any Apache based webserver (but check that the RewriteRule doesn’t clash with anything else you have setup).

Related Posts Plugin for WordPress, Blogger...

Tags

Filed under Configuration.

Tags: , , ,

  • Getting a redirect to the same page with a 500 error. Yourls 1.5 on a shared host account.

  • I’m getting sent to the root.I tried http://wrdpg.us/twitter.qr and I get redirected to http://wrdpg.usAny ideas?
    I am using YOURLS 1.5

    • Hi there Glen and Enkerli,

      I’m running YOURLS 1.4 and Apache on my own webserver. I haven’t had time yet to upgrade to 1.5 and make sure the above solution still works.

      It should though, as I’m not doing anything to YOURLS, it’s working at the web server/apache level – what is important though is that the redirect rule is before the YOURLS rules (and that it doesn’t “clash” with any other rule you’ve got)

  • Maybe the rules from 1.5 clash with these QR code rules? I’m no Apache expert, so I’m not clear on what the clashes look like.

    From scratch, my .htaccess looks like this, after Yourls installation:

    # BEGIN YOURLS

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /yourls-loader.php [L]

    # END YOURLS

    The QR code rule goes where?
    Sorry for the newbie question but I’m sure it can help others.

    • Put the QR code ReWriteRule immediately before the yourls-loader.php ReWriteRule.

      • That’s what I did. I now get a 500 on a shortened URL and a 302 on the .qr version.
        Here’s my modified .htaccess:

        # BEGIN YOURLS

        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^([0-9A-Za-z]+).qr/?$ http://chart.apis.google.com/chart?cht=qr&chs=200×200&chl=http://%{HTTP_HOST}/$1
        RewriteRule ^(.*)$ /yourls-loader.php [L]

        # END YOURLS

        Can .htaccess conflict with one another? Doesn’t the local one take precedence?

  • Joe

    You’re missing the LAST directive from your rewite. Without it, Apache will continue until it hits the default YOURLs statement and execute that instead – which will redirect you to the root. Here is the fixed version:
    RewriteRule ^([0-9A-Za-z]+).qr/?$ http://chart.apis.google.com/chart?
    cht=qr&chs=200×200&chl=http://%{HTTP_HOST}/$1 [L]

  • Toddlrice

    Hello, i am a noob but curious as to how this would work>>>  I am wanting to host my qrcodes, I created some for offline campaign but the provider is now down and lost my tracking and qr directs..
    How can i take the codes I have and implement them into this so i can control the qr codes on my doimain??/
    THX

    • Hi Todd, if you encoded someone else’s short url in the code; then there’s nothing much you can do for codes you have already produced. This is the advantage of using your own domain/short URLs in QR Codes – you control them and can point them where ever you want.

Theme Beantin created by James Royal-Lawson. Privacy Policy