<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>NearlyFreeSpeech.NET Blog &#187; Tips &amp; Tricks</title>
	<atom:link href="http://blog.nearlyfreespeech.net/category/tips-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.nearlyfreespeech.net</link>
	<description>A blog from the staff at NearlyFreeSpeech.NET.</description>
	<pubDate>Wed, 19 Nov 2008 21:22:44 +0000</pubDate>
	
	<language>en</language>
			<item>
		<title>Surprise WordPress Upgrade</title>
		<link>http://blog.nearlyfreespeech.net/2008/04/08/surprise-wordpress-upgrade/</link>
		<comments>http://blog.nearlyfreespeech.net/2008/04/08/surprise-wordpress-upgrade/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 04:30:21 +0000</pubDate>
		<dc:creator>jdw</dc:creator>
		
		<category><![CDATA[Tips &amp; Tricks]]></category>

		<category><![CDATA[Updates &amp; Upgrades]]></category>

		<guid isPermaLink="false">http://blog.nearlyfreespeech.net/?p=42</guid>
		<description><![CDATA[We received a note from Technorati today about a serious security problem with old versions of WordPress, including the version we were running, that is now being exploited on a widespread scale.  We&#8217;ve thus hastily upgraded to WordPress 2.5.  That did cause a brief bit of disruption to the &#8220;News &#038; Announcements&#8221; portion [...]]]></description>
			<content:encoded><![CDATA[<p>We received a note from Technorati today about a serious security problem with old versions of WordPress, including the version we were running, that is now being exploited on a widespread scale.  We&#8217;ve thus hastily upgraded to WordPress 2.5.  That did cause a brief bit of disruption to the &#8220;News &#038; Announcements&#8221; portion of our member site, which is now resolved.</p>
<p>If you want to run WordPress, you too may want to check whether you&#8217;re running the most current version with the latest patches.  Better safe than sorry!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nearlyfreespeech.net/2008/04/08/surprise-wordpress-upgrade/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Writing files in PHP</title>
		<link>http://blog.nearlyfreespeech.net/2007/01/28/writing-files-in-php/</link>
		<comments>http://blog.nearlyfreespeech.net/2007/01/28/writing-files-in-php/#comments</comments>
		<pubDate>Sun, 28 Jan 2007 18:08:24 +0000</pubDate>
		<dc:creator>jdw</dc:creator>
		
		<category><![CDATA[Tips &amp; Tricks]]></category>

		<guid isPermaLink="false">http://blog.nearlyfreespeech.net/2007/01/28/writing-files-in-php/</guid>
		<description><![CDATA[The &#8220;traditional&#8221; web server just reads and sends out files in response to incoming requests. Consequently, the standard security configuration is therefore set up to give web accesses the bare minimum in terms of file permissions: the ability to read the site&#8217;s files, but not to change them. 
But many PHP applications want to write [...]]]></description>
			<content:encoded><![CDATA[<p>The &#8220;traditional&#8221; web server just reads and sends out files in response to incoming requests. Consequently, the standard security configuration is therefore set up to give web accesses the bare minimum in terms of file permissions: the ability to read the site&#8217;s files, but not to change them. </p>
<p>But many PHP applications want to write files as well: forums that support uploading files, CMS applications, and many Wikis all create or update files as a normal part of their operation. Since the default permissions don&#8217;t allow it, many people run into trouble when trying to develop or install PHP applications that need this ability. This blog post will attempt to show how to do this on our system in a way that is easy to set up and very secure.<br />
<span id="more-22"></span><br />
On Unix systems like ours, each file has two owners: a user and a group. On our system, the user who owns your files is usually identified to you as “me” and the web server runs as user “web.” Similarly, there are two groups: “me” and “web.” When you are logged in to the ssh server, your default group is group “me” but you are a member of both “me” and “web.” The web server, on the other hand, is limited to the “web” group.</p>
<p>PHP safe_mode imposes a number of restrictions above and beyond standard Unix filesystem permissions. For the most part, these restrictions are designed to prevent multiple sites on a shared host (like ours) from trampling each other. However, if applied properly, they also provided opportunities for you to limit damage to your own site in the event of a bug or exploitable security flaw.</p>
<p>The secret to using this feature properly is to set the proper group ownership on your PHP scripts. If your PHP script is intended to read and write other files in your web space (or related directories), then the script should be owned by the “web” group.</p>
<p>In order for the web server to write to files or directories in your web space, it needs write permissions. As far as the Unix filesystem goes, there are two ways for it to get that: the file can have group “web” and be group writable, or it can have group “me” and be world writable. However, with PHP, that is not sufficient.</p>
<p>On our system, for PHP to be able to write to a file, the group of the file (or parent directory, if creating a file) must match the group of the PHP script doing the writing. If you want a PHP script to be able to write files, you should take the additional step of setting that script’s group to “web.&#8221; You’ll also need to make sure that the destination directory for created files has group “web” and that it is group writable.</p>
<p>If this is set up properly, these two steps form a sort of interlock: you specify which scripts are permitted write files, you specify where files are permitted to be written, and the only combination that will succeed is if a permitted script tries to write to a permitted location. Everything else is off limits. This effectively protects the rest of your files from being unexpectedly overwritten by your file-writing scripts, <i>and</i> it prevents other PHP scripts from writing files at all, even if they later turn out to have a security problem that might otherwise allow it.</p>
<p>It’s also possible to get PHP to write files by setting the target file (or directory) to group “me” and giving it world write permissions. However, doing so forgoes all of the above protection, and so it is not recommended. You can also run into problems with this approach if the PHP script intends to create a directory and then create a file in that directory.</p>
<p>All of our member sites have a “protected” directory at the same level as the “public” (aka “htdocs” for older sites) directory that contains your web-accessible material. The “protected” directory cannot be directly accessed via the web, but it has the appropriate ownership and permissions already set for PHP scripts with group “web” to be able to write files. This makes it an ideal, safe place for your site to store and maintain support files without having to worry about what access controls are needed to prevent visitors from accessing those support files directly over the web.</p>
<p>As a final caveat, make sure the PHP scripts you set to group &#8220;web&#8221; are not group-writable, because that would grant the server permission to modify the script itself, which is generally undesirable.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nearlyfreespeech.net/2007/01/28/writing-files-in-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Forwarding sites &#038; URL rewriting</title>
		<link>http://blog.nearlyfreespeech.net/2006/11/17/forwarding-sites-url-rewriting/</link>
		<comments>http://blog.nearlyfreespeech.net/2006/11/17/forwarding-sites-url-rewriting/#comments</comments>
		<pubDate>Fri, 17 Nov 2006 05:23:30 +0000</pubDate>
		<dc:creator>jdw</dc:creator>
		
		<category><![CDATA[Tips &amp; Tricks]]></category>

		<guid isPermaLink="false">http://blog.nearlyfreespeech.net/2006/11/17/forwarding-sites-url-rewriting/</guid>
		<description><![CDATA[We recently got a support inquiry about alternate methods of forwarding visitors from one URL to another.
We have a FAQ entry about using &#8220;decoy&#8221; sites to forward alternate URLs.  This is one method, and probably the easiest, but there are many others that can be useful in different circumstances.
What I&#8217;d like to do here [...]]]></description>
			<content:encoded><![CDATA[<p>We recently got a support inquiry about alternate methods of forwarding visitors from one URL to another.</p>
<p>We have <a href="https://members.nearlyfreespeech.net/support/faq?q=ForwardSite#ForwardSite">a FAQ entry</a> about using &#8220;decoy&#8221; sites to forward alternate URLs.  This is one method, and probably the easiest, but there are many others that can be useful in different circumstances.</p>
<p>What I&#8217;d like to do here is talk about the reasons why we recommend this one and discuss some of the alternatives and when they might be more useful.<br />
<span id="more-14"></span><br />
The premise of URL forwarding is that a web site should have a single canonical name. Other names might exist, but they&#8217;re usually alternate names, or variant spellings, or similar.  These names might be common typos you want to catch, easier to type, or whatever. But the actual site accesses should use the canonical name, because that&#8217;s best for search engine rankings and long-term recognition of your site.</p>
<p>By far the most common case is when you have a domain name, such as example.com, and you want to use http://www.example.com/ and http://example.com/ to access the site, so that&#8217;s the case I&#8217;ll focus on here.  I&#8217;ll also assume that www.example.com is the canonical name of choice. (The temptation to use the bare domain can be very high for some people, but see <a href="https://members.nearlyfreespeech.net/support/faq?q=BareDomain#BareDomain">this FAQ entry</a> for a discussion of reasons why we recommend against it.)</p>
<p>Our FAQ entry suggests creating a forwarding site, assigning example.com to it as an alias, and creating a one-line .htaccess file in its htdocs directory:</p>
<pre><code>RedirectPermanent / http://www.example.com/</code></pre>
<p>We generally recommend creating an alternate site because this is the most efficient approach.  With this method, people who enter one of the &#8220;wrong&#8221; names hit the decoy site and get redirected to the canonical name right up front, and there&#8217;s zero extra overhead on any subsequent requests. It&#8217;s also really easy to do, and it&#8217;s impossible to screw up your working site while setting it up.</p>
<p>Second, we recommend the RedirectPermanent Apache directive for two reasons. The &#8220;permanent&#8221; redirect code (301) helps keep search engines and the like from continuously trying to index the &#8220;wrong&#8221; name instead of the right one. Also, the Apache Redirect family works for subordinate URLs.  One of the most common redirection mistakes is a setup that will correctly redirect http://example.com/ to http://www.example.com/, but won&#8217;t redirect http://example.com/something.html to http://www.example.com/something.html.  RedirectPermanent is a one-line solution that handles such cases flawlessly.</p>
<p>One thing that&#8217;s come up a couple of times is that once you have a forward for one alternate name, you can add as many other alternate names for the same site as you want.  This is easily done by just adding the alternate aliases to the forwarding site.</p>
<p>Now, let&#8217;s take a look at an entirely different approach.  The ultimate tool for URL rewriting is, of course, the Apache rewrite module, mod_rewrite.  It&#8217;s possible to use it to accomplish the exact same thing, but without the use of a second site.  To do so, you just create an .htaccess file in the site root containing the following:</p>
<pre><code>RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^.*$ http://www.example.com%{REQUEST_URI} [R=301,L]</code></pre>
<p>To summarize how this works in a nutshell:</p>
<p>1) &#8220;RewriteEngine on&#8221; is needed to enable mod_rewrite.</p>
<p>2) RewriteCond applies a condition under which rewriting will occur. In this case, we want to rewrite if the %{HTTP_HOST} (the content of the HTTP Host: header, which is where the requested domain name lives) does not (the !) match www.example.com from beginning to end (the ^ and $, respectively), without regard to uppercase or lowercase (the [NC]).  So this will skip www.example.com and www.ExAmPlE.com but not example.com or any other alias you might want to add.</p>
<p>3) This RewriteRule applies to the whole URL, no matter what it is (the ^.*$) and changes it to the correct name (the http://www.example.com)  while keeping the same URI path (the %{REQUEST_URI}) and sends it back to the client as a 301 redirect (the R=301) and skip any other rewrite rules (the L).</p>
<p>The possible big advantage for this method is that it effectively suppresses the use of the default site names we provide (example.nfshost.com), if that&#8217;s important to you.  It also doesn&#8217;t require the existance of an alternate site, if you find that distasteful for some reason. The disadvantage is the extra overhead, which applies to absolutely every request, whether it&#8217;s using a URL that needs rewriting or not. (Also, as I personally don&#8217;t care for mod_rewrite I consider the use of it a fundamental drawback, but that is more bias on my part than a viable objection.)</p>
<p>The specific question we got was about creating a rewrite site that can handle multiple independent destinations. In this situation, you have a large number of names to rewrite <em>to</em> as well as <em>from</em>. In other words, not just redirecting http://example.com/ to http://www.example.com/ but also redirecting http://other.com/ to http://www.other.com/, all from a single site. RedirectPermanent isn&#8217;t smart enough to handle that, but mod_rewrite is.</p>
<p>For this approach, create one &#8220;generic&#8221; forwarding site from the &#8220;sites&#8221; tab, and create an .htaccess file in that site&#8217;s htdocs directory that looks like this:</p>
<pre><code>RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^.*$ http://www.example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^other.com$ [NC]
RewriteRule ^.*$ http://www.other.com%{REQUEST_URI} [R=301,L]</code></pre>
<p>You can add more sites (to and from) by repeating the RewriteCond and RewriteRule lines.  The only real difference between this and the previous example is that we ditched the ! in the RewriteCond lines, meaning that instead of applying to names that <em>don&#8217;t</em> match, we apply to names that <em>do</em>.</p>
<p>So that&#8217;s three different approaches to the same problem. All work, but they are useful in different circumstances. We can&#8217;t document everything in our FAQ, but I&#8217;m hoping that one of the uses of our new blog will be the opportunity to explore alternatives like this that might be helpful to our members.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nearlyfreespeech.net/2006/11/17/forwarding-sites-url-rewriting/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
