randys.org

Wasting your precious bandwidth since 1999

Posts Tagged ‘apache’

Changing Permalinks In Typo

When I switched from Wordpress to Typo, I faced the issue of keeping my Wordpress permalinks in Typo. Unfortunately, there’s no simple way of doing this. I noticed the ‘redirects’ table in the database, but I could never get it to realy do anything. So, I had to dig in and find the bit of code that controlled the permalinks in Typo.

Typo uses ‘articles’ as it’s base for all posts so I looked in config/routes.rb and found what I needed to change. As you can see from the url of this post, I use ‘content’ as the base of my permalink. So, I changed all the references of ‘articles’ to ‘content’ and all seems to be right in the world.

I changed

1
2
map.connect 'articles',
    :controller => 'articles', :action => 'index'

to

1
2
map.connect 'content',
    :controller => 'articles', :action => 'index'

and then changed

1
2
3
map.connect 'articles/:year/:month/:day',
    :controller => 'articles', :action => 'find_by_date',
    :year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/

to

1
2
3
map.connect 'content/:year/:month/:day',
    :controller => 'articles', :action => 'find_by_date',
    :year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/

and so on.

The only thing I haven’t gotten to work is to map the pages. I can change the ‘pages’ map, but it doesn’t act as desired. I only had 3 pages in Wordpress, so I might as well either alias them or premanent redirect in the Apache configuration.

If anybody has some insight, I’d love to here it. Also, still haven’t figured out why mod_rewrite is working like it should.

• • •

Apache2: mod_rewrite vs. mod_proxy

I can’t quite figure this out. I’m trying to write a simple rule to force randys.org to redirect to www.randys.org but it’s not realy working. Of course, I haven’t tried it on a vhost that isn’t proxying request to mongrel. Here’s the rule:

RewriteEngine On
RewriteCond %{HTTP_HOST}    !^www.randys.org$ [NC]
RewriteCond %{HTTP_HOST}    !^$
RewriteRule ^/(.*)          http://www.randys.org/$1 [R=301,L]

Anyone?

• • •

Typo, Apache & Mongrel, Oh My!

Wow. I’ve been testing the possibility of running Typo on my VPS with the pre-notion that it just wouldn’t work very well. I think I may be wrong. It’s been running for about a week while I tweak some shit and so far, my memory performance has been stable. Of course, thing would probably be different if I threw some real traffic at it… but, then again, I don’t get a lot of that.

The Setup

Initially, I was going to use lighty + fastcgi to serve up the Rails application. So, I had setup lighty to serve up my PHP/MySQl sites and that worked just fine. Then I started reading up on Mongrel and wanted to see what all the hubub was about. But then I got to reading the Mongrel site (more specifically the part where he says don’t use lighty and mod_proxy).

Crap.

Aight, so back to Apache and their proxy setup. no biggie. I racked my brain for several hours one night trying to figure out why the hell I kept getting these 403 Forbidden errors in Apache. Hours. After the millionth Google search, I finally found the issue: The mod_proxy configuration in Ubuntu is turned off by default (well, not really turned off… it just denies traffic to the proxy server).

 8     <Proxy *>
 9         Order deny,allow
10         #Deny from all
11         Allow from .randys.org
12     </Proxy>

Line 8 was initially not commented out. I had to comment that out and add the Allow from .randys.org bit.

Once I got that changed and reloaded Apache, everything is working nicely together.. even PHP.

The Tweaks

So far, the only tweaks I’ve managed to make is to the flickr Sidebar plugin. It now uses Lightbox V2. That was a bit tricky, considering I don’t really know Ruby but, it all worked out in the end.

As I figure out more stuff to do, I’ll post more.

• • •

Google Friendly URLs with PHP and Apache

Dynamic websites are essential for content heavy sites and chances are, you’re not going to create one static page for every single page/article/product item you have stored in a database. This means you’re going to need to pass parameters to your script to pull the right content from your database. Up until recently, most people didn’t give much thought to those nasty URLs with all those ampersands (&) and equal signs and how they affect spidering by search engines. While these URLs are perfectly valid, they tend not to get indexed by Google (and other search engines) unless you submit each URL for indexing (which can cost you money). And these days, if you’re not in Google, you’re not being found.

Enter PHP and Apache

<p><p>If you&#8217;re using <span class="caps">PHP</span> and Apache, you can still be dynamic and be spidered by all the popular search engines fairly easily. Of course, this depends on your hosting provider and how much control they let you have in terms of htaccess. <span style="font-style: italic;">.htaccess </span>files let you manipulate some Apache settings on a per host/per folder basis. Have a look at the <a href="http://httpd.apache.org/docs/">Apache manual</a> for a full listing of configuration settings for Apache web server.</p></p>


<p><p>Now for the goods. Here&#8217;s a basic rundown of how search engine friendly URLs can be used in a dynamic way. Let&#8217;s say you have a catalog of items, and your script takes 3 parameters: &#8216;section&#8217; , &#8216;action&#8217;, and &#8216;item.&#8217; Normally, when passing parameters to a script, your url will look something like this:</p></p>


<p><pre><code>/index.php?section=widgets&#38;action=view&amp;amp;item=327</code></pre></p>


<p><p>With the proper directives in an Apache .htaccess file and some simple <span class="caps">PHP</span> scripting, you can turn that <span class="caps">URL</span> into something like this:</p></p>


<p><pre><code>/catalog/section/widgets/action/view/item/327</code></pre></p>


<p><p>Looks like a bunch of folders, but it&#8217;s not. &#8216;catalog&#8217; is actually a file (a <span class="caps">PHP</span> file) without the extention, and with the help of a <span style="font-style: italic;">.htaccess</span> file,  Apache treats it like a <span class="caps">PHP</span> script.  Here&#8217;s what the <span style="font-style: italic;">.htaccess</span> file looks like:</p></p>

ForceType application/x-httpd-php

<p><p>That&#8217;s it. Nothing too complicated. Now for the trickier part, grabbing those parameters from the <span class="caps">URL</span> with <span class="caps">PHP</span>.</p></p>


<p><p>There&#8217;s a server variable that gets passed to the script calle &#8216;PATH_INFO&#8217; which contains the entire string after the catalog file (including that first slash). To grab it in a <span class="caps">PHP</span> script, you&#8217;d use the <a href="http://us2.php.net/manual/en/reserved.variables.php#reserved.variables.server">$_SERVER</a> global variable (a la $_SERVER[&#8216;PATH_INFO&#8217;]). The idea is to <a href="http://us2.php.net/manual/en/function.explode.php">explode</a> the string into an array and loop through them to get the parameters. Here&#8217;s a small function to do so:</p></p>


<p><pre><code>&lt; ?php

function getArgs() { $params = explode(”/”, $_SERVER['PATH_INFO']); } ?>

<p><pre><code>for($i = 1; $i &lt; sizeof($params); $i = $i + 2)

{ $args[$params[$i]] = $params[$i+1]; } return $args;

<p><p>When used on the above example, this function will return an array like so:</p></p>


<p><pre><code>Array (
 [section] =&gt; widgets
 [action] =&gt; view
 [item] =&gt; 327

)

<p><p>Implimenting this into a current site shouldn&#8217;t be too hard. The &#8216;catalog&#8217; script would simply be a wraper script where you can pass the required values into existing code.</p></p>


<p><p>If you find that your web host doesn&#8217;t allow use of .htaccess files, or they don&#8217;t allow the &lt;Files&gt; directive to be used, you can do one of two things (and I highly recommend the first).</p></p>


<p><p>1) Change hosts! I use <a href="http://www.dreamhost.com/rewards.cgi?sesser">Dreamhost</a> and they are extremely flexible, have an awsome support team and have a <a href="http://www.dreamhost.com/rewards.cgi?sesser/shared/comparison.html">very good deal for $9.95/mo</a>.</p></p>


<p><p>2) Just use a normal <span class="caps">PHP</span> script (i.e. catalog.php) and use the same function. While this method works, I&#8217;ve read that some search engines see this as a hack/cheat and might not spider the pages anyway. However, if you are a reputable business, you can usually contact them and get your pages indexed.</p></p>

Other Web Servers

<p>If you&#8217;re running <span class="caps">IIS</span> (god forbid), there is hope for you. There is at least one product (ISAPI) available that mimics the Apache mod_rewrite module. This <span class="caps">ISAPI</span> can help do the same thing (although it won&#8217;t be quite as easy). But the drawbacks are that a) the <span class="caps">ISAPI</span> isn&#8217;t free and b) if you&#8217;re in a shared environment, you can&#8217;t use it. There are other ways to manipulate your <span class="caps">IIS</span> settings to use search engine friendly URLs, but they all have their downsides. Bottom line: stop using <span class="caps">IIS</span>.</p>


<p>As for other servers, I can&#8217;t really say. Chances are there are some tweaks that can be made to the configuration and/or scripts. If you know of any, please drop me a line and I&#8217;ll try to get it posted.</p>
• • •

All content Copyright © Randy Sesser | Hosted by WebFaction
Entries (RSS) | Comments (RSS)