Simplify your URLs

Posted on June 12th, 2008 by dandavis

I hate long URLs. Hell, I even hate extensions on my pages.

For some sites I've done, I did not want to have a bunch of individual pages:

  • /index.php
  • /help.php
  • /about.php

I wanted to use Smarty, a template engine for PHP, but without having to create individual PHP files and without long URLs (/index.php?page=about).

So, in my index.php file:

<?php
$smarty = new Smarty();
list ($page,$query) = explode("?",$HTTP_SERVER_VARS["REQUEST_URI"]);
$page = basename($page, ".php");
$smarty->assign('page', $page);
$smarty->display('index.tpl');
?>

(I stripped a bunch out, of course. For example, you never want to take what a user tells you at face value! My index.php file cleans up $page and compares it against a list of valid pages before doing anything else.)

Then, in index.tpl, I have a basic structure in place and I use Smarty's {include} function to include 'page' in the correct location:

<html>
<head>
<title>Common Title - {$page}</title>
</head>
<body>
Some common content, perhaps.
{include file="$page.tpl"}
</body>
</html>

And, then, to get rid of index.php… so that /about loads the about.tpl content, etc, in .htaccess I have:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]
 
RewriteCond %{SCRIPT_FILENAME} ! -f
RewriteCond %{SCRIPT_FILENAME} ! -d
RewriteRule ^(.+)$ /index.php?$1 [PT,L,QSA]
</IfModule>

That first part, where I redirect www.mydomain.com to mydomain.com, is because 'www.' is redundant and its use has been deprecated!

The second part has the bits that are relevant to this post…

RewriteCond %{SCRIPT_FILENAME} ! -f
RewriteCond %{SCRIPT_FILENAME} ! -d

If the user is requesting an actual file or directory, then we won't try to rewrite the URL of the request to meet our own perverted neatness requirements.

RewriteRule ^(.+)$ /index.php?$1 [PT,L,QSA]

The URL that the user requested, not being an actual file or directory, is rewritten to be a query string to index.php.

So, for example, if the user requested http://mydomain.com/about, this rewrite would cause Apache to serve up http://mydomain.com/index.php?about (without rewriting the address in the browser). index.php would parse the string, set $page = 'about' and load up the index.tpl Smarty template. Smarty would parse the template and include about.tpl.

There! That was overly complex and convoluted, but at least my URLs remain short and I can add new pages (or update common bits of code) without a bunch of copying, pasting, and editing. And that makes me happy.

Hiding referral links

Posted on June 12th, 2008 by dandavis

When you run across a referral link to a product (at Amazon.com, for example), are you the kind of user that copies the link and replaces the referral code with your own or a friends?

Yeah, me too! I hate users like us!

There might be packages or code snippets out there for this already, but I wrote my own 'cause that's what I do.

First, I wrote a file (externallinks.php) that has a list of referral links… this could be done in a database, but that was overkill for me.

<?php
$url['amazon-product-1'] = 'http://www.amazon.com/exec/obidos/ASIN/B0015AR7AK/dandaviscom-20';
$url['amazon-product-2'] = 'http://www.amazon.com/exec/obidos/ASIN/1591862027/dandaviscom-20';
# Not sure if this next one works like I think it does...
# but it's a whole lot shorter than the one that I *know* works.
$url['default'] = 'http://www.amazon.com/?tag=dandaviscom-20'];
?>

Once you have your list of external links, you would setup your external.php (or whatever you want to call it)…

<?php
require_once('externallinks.php');
if (isset($_REQUEST['link']) && !empty($_REQUEST['link'])) {
   $link = $_REQUEST['link'];
   if (isset($url[$link]) && !empty($url[$link])) {
      Header('Location: '.$url[$link]); exit;
   }
}
echo 'Give the user some error about there not being a valid link. You know... to be helpful and all...';
exit;
?>

Now, in your pages, instead of putting in the direct links, you can link to this page.

For example, instead of:

http://www.amazon.com/exec/obidos/ASIN/B0015AR7AK/dandaviscom-20

You would link to:

http://yourdoamin.com/external.php?link=amazon-product-1

Now, once the user gets to Amazon (or wherever), then they could change the referral code… unless that site uses some sort of redirector. Amazon.com has one: http://www.amazon.com/gp/redirect.html.

Perpetual Copyright

Posted on June 12th, 2008 by dandavis

Seen this a few times while stumbling. I've been doing it myself for quite a while now, but…

Put this code in the footer of your pages:
<?php
$copyright = '2005'; $year = date('Y');
if ($year > $copyright) { $copyright .= '-'.$year; }
echo 'Copyright &copy; ',$copyright,' Me';
?>

Produces:

Copyright © 2005-2008 Me

And, as other sites are keen on pointing out, on January 1st of the new year, your copyright text will automatically update… So, January 1st, 2009, the code about will print out:

Copyright © 2005-2009 Me

Etc, etc.

Defining CSS constants using PHP

Posted on December 23rd, 2005 by dandavis

I stumbled upon this article (article has sinced been archived here) about using PHP to define constants in CSS. I bookmarked it and finally got back to reading it this morning. I don't really have a huge need for doing this, but I have had to hack enough CSS to change color schemes that I wish that this was used in more places!

Go take a glance at this guy's article, but there is an easier way to implement constants than Tyler has shown… of course, there may be a method to his madness that escapes me.

Here's how I did it…

1. Rename your CSS file(s) as PHP: mv style.css style.php

2. Edit your style.php file thusly:

<?php
header('content-type:text/css');
include('css_constants.php');
print <<<END
// Original CSS code here
END;
?>

3. Go through your original CSS and replace common styles and colors with variables. Define those variables in css_constants.php.

css_constants.php:
$background_color = '#cca';

4. Now you go back through style.php and substitute $background_color where originally you had #caa.

style.php:
#nav {
background:$background_color
}

If I can find the time, I'll finish up my own conversions and post links to the source. Maybe. We'll see. However, if any of this really matters to you, you'll have it figured out and implemented before I get a round toit.