Clean URLs Part 2
Posted on
My previous article, Clean URLs with htaccess shows a very basic way of going about cleaning up your URLs. Now, let’s assume that not all of your pages are meant to look or act the same.
- How can we use different keywords, title, and description on each page?
- How can we use different Javascript or CSS files on different pages?
- How can we use add different header content for one or more pages?
clean.php
This is what a typical PHP file will look like. We will store the title, keywords, etc. as variables to use in the index.php, and the content will be in plain HTML or HTML and PHP.
<?php
$title = 'Clean URLs Part II';
$keywords = 'web design,web development,html,xhtml,html5,css,php,htaccess';
$description = 'How to use .htaccess and basic PHP to create clean web addresses';
$js = array('jquery', 'effects');
$header = "<ol>
<li><a href='#clean_php'>Go to clean.php code</a></li>
<li><a href='#htaccess'>Go to .htaccess code</a></li>
<li><a href='#index_php'>Go to index.php code</a></li>
</ol>";
?>
<h2>Clean URLs Part II</h2>
<div id='clean_php'>Content...</div>
<div id='htaccess'>Content...</div>
<div id='index_php'>Content...</div>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?$ index.php?section=home&%{QUERY_STRING} [L]
RewriteRule ^([^/\.]+)?$ index.php?section=$1&%{QUERY_STRING} [L]
RewriteRule ^([^/\.]+)?/([^/\.]+)?$ index.php?section=$1&page=$2&%{QUERY_STRING} [L]
RewriteRule ^([^/\.]+)?/([^/\.]+)?/([^/\.]+)?$ index.php?section=$1&page=$2&title=$3&%{QUERY_STRING} [L]
</IfModule>
There’s a bit of PHP you will need to put above your HTML in your index.php. This code will actually execute the PHP script clean.php and store the output in a variable called $page_contents to be used later. This will give us access to $title, $keyword, and whatever other variables are set in code.php.
index.php:
<?php
include("lib/const.php");
if (isset($_GET['page'])) $page = $_GET['page'];
if (isset($page)) {
if (!file_exists("{$page}.php"))
$page = '404';
ob_start();
include("{$page}.php");
$page_contents = ob_get_contents();
ob_end_clean();
}
if (isset($layout) && $layout === false) :
echo $page_contents;
else :
?>
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<title><?php if (isset($title)) echo "{$title} - " ?>My Site</title>
<meta name="keywords" content="<?= isset($keywords) ? $keywords : "web design,photography,art" ?>" />
<meta name="description" content="<?= isset($description) ? $description : "Justin Silva's Blog" ?>" />
<?php
if (isset($css)) {
foreach ($css as $style) {
echo "<link rel="stylesheet" type="text/css" href="/css/{$style}.css" />";
}
}
?>
<?php
if (isset($js)) {
foreach ($js as $script) {
echo "<script type='text/javascript' src='/js/{$script}.js'></script>";
}
}
?>
</head>
<body>
<header>
<h1>My Site</h1>
<?php if (isset($header)) echo $header ?>
</header>
<section>
<?= $page_contents ?>
</section>
<footer>
©Justin Paul Silva
</footer>
</body>
</html>
You can customize this to add an id attribute to the body tag for styling, add something to the footer, etc.
HTML Result
This is what the browser should see when you go to mysite.com/clean:
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<title>Clean URLs Part II - My Site</title>
<meta name="keywords" content="web design,web development,html,xhtml,html5,css,php,htaccess" />
<meta name="description" content="How to use .htaccess and basic PHP to create clean web addresses" />
<script type='text/javascript' src='/js/jquery.js'></script>
<script type='text/javascript' src='/js/effects.js'></script>
</head>
<body>
<header>
<h1>My Site</h1>
<ol>
<li><a href='#clean_php'>Go to clean.php code</a></li>
<li><a href='#htaccess'>Go to .htaccess code</a></li>
<li><a href='#index_php'>Go to index.php code</a></li>
</ol>
</header>
<section>
<h2>Clean URLs Part II</h2>
<div id='clean_php'>Content...</div>
<div id='htaccess'>Content...</div>
<div id='index_php'>Content...</div>
</section>
<footer>
©Justin Paul Silva
</footer>
</body>
</html>
There’s a lot of code here. Let me know if it doesn’t all work as expected.