login/register

Snip!t from collection of Alan Dix

see all channels for Alan Dix

Snip
summary

I’ve been thinking about individual features of variou...
To start with we’ll redirect all requests to a single ...
view source
print?
... _route_) that contains the directory portion of the URL.

PHP URL Routing (PUR) || Joseph Scott
http://josephscott.org/archives/2008/11/php-url-routing-pur/

Categories

/Channels/techie/web development

[ go to category ]

For Snip

loading snip actions ...

For Page

loading url actions ...

I’ve been thinking about individual features of various code frameworks, starting with two features that are closely related: clean URLs and URL routing. To examine this idea further I started writing a basic implementation of these two features in PHP.

To start with we’ll redirect all requests to a single index.php file. Here’s the .htaccess file:

1.RewriteEngine on
2.RewriteBase /
3.RewriteCond %{REQUEST_FILENAME} !-f
4.RewriteCond %{REQUEST_FILENAME} !-d
5.RewriteRule ^(.*)\?*$ index.php?_route_=$1 [L,QSA]

The idea here is pretty basic, unless the exact file or directory exists redirect the request to index.php. When the redirect happens, add a GET variable (_route_) that contains the directory portion of the URL.

HTML

<p>I&#x2019;ve been thinking about individual features of various code frameworks, starting with two features that are closely related: clean URLs and URL routing. To examine this idea further I started writing a basic implementation of these two features in PHP.</p> <p>To start with we&#x2019;ll redirect all requests to a single index.php file. Here&#x2019;s the .htaccess file:</p> <div class="syntaxhighlighter" id="highlighter_34653"><div class="bar"><div class="toolbar"><a class="item viewSource" style="width: 16px; height: 16px;" title="view source" href="#viewSource">view source</a><div class="item copyToClipboard"><embed id="highlighter_34653_clipboard" type="application/x-shockwave-flash" title="copy to clipboard" allowscriptaccess="always" wmode="transparent" flashvars="highlighterId=highlighter_34653" menu="false" src="http://josephscott.org/wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/clipboard.swf" height="16" width="16"></div><a class="item printSource" style="width: 16px; height: 16px;" title="print" href="#printSource">print</a><a class="item about" style="width: 16px; height: 16px;" title="?" href="#about">?</a></div></div><div class="lines"><div class="line alt1"><code class="number">1.</code><span class="content"><span class="block" style="margin-left: 0px ! important;"><code class="plain">RewriteEngine on</code></span></span></div><div class="line alt2"><code class="number">2.</code><span class="content"><span class="block" style="margin-left: 0px ! important;"><code class="plain">RewriteBase /</code></span></span></div><div class="line alt1"><code class="number">3.</code><span class="content"><span class="block" style="margin-left: 0px ! important;"><code class="plain">RewriteCond %{REQUEST_FILENAME} !-f</code></span></span></div><div class="line alt2"><code class="number">4.</code><span class="content"><span class="block" style="margin-left: 0px ! important;"><code class="plain">RewriteCond %{REQUEST_FILENAME} !-d</code></span></span></div><div class="line alt1"><code class="number">5.</code><span class="content"><span class="block" style="margin-left: 0px ! important;"><code class="plain">RewriteRule ^(.*)\?*$ index.php?_route_=$1 [L,QSA]</code></span></span></div></div></div> <p>The idea here is pretty basic, unless the exact file or directory exists redirect the request to index.php. When the redirect happens, add a GET variable (_route_) that contains the directory portion of the URL.</p> <p></p>