Category Archives: Apache

PHP Code Snippet

Because of the re-design, there’s a lot of broken links – mainly because the blog used to live under the /whatithink/ folder on the server and now resides in the root folder. So all those Google links now point to a part of the site that no longer exists.

So anyone coming here from Google is going to get a 404 error. Not good.

While I get my head around using .htaccess to automatically rewrite the broken URLs, I’ve created a 404 page that will, (hopefully), correct the URL.

Here’s the PHP:

$requested = $_SERVER['REQUEST_URI'];$parts = explode('/', $requested);foreach ($parts as $part) {switch($part) {case ('whatithink'):break;case (''):break;default:$out .= '/' . $part;}}$out = 'http://' . $_SERVER['HTTP_HOST'] . $out;

Because all my broken URLs have an extra “/whatithink/” section, I can just break the requested URL into parts, and rebuild it without that section.

“The connection was refused”Error Message When Using Leopard’s Web Sharing

I came across this problem when trying to enable Web Sharing on my Macbook:

Connection Refused Error

Connection Refused Error

Seems that even if the Sharing Preferences Pane shows that Apache is running, it’s not. A simple Terminal command will show you:

$ ps aux | grep httpd
pmac 1796 0.0 0.0 599820 464 s001 R+ 12:52pm 0:00.00 grep httpd

After a lot of searching, I found the solution. There’s an issue with the Leopard Apache upgrade that somehow forgets to create the log files that Apache needs to start. To fix the problem, type the following commands into Terminal:

sudo touch /var/log/apache2/access_log
sudo touch /var/log/apache2/error_log
sudo chmod 600 /var/log/apache2/access_log
sudo chmod 600 /var/log/apache2/error_log

Restart the Web Sharing Service by unticking and ticking the box is System Preferences or by running the following command in Terminal:

sudo apachectl restart

Apache should be up and running and you can test this by clicking this link: http://localhost. If everything goes to plan you should see the following page:

Apache Start Page

Apache Start Page

Setting Up Movable Type 4 on a MAMP Server

Recently my hosting provider, Blacknight Solutions, requested that bloggers using WordPress use the WP-Cache plugin to reduce the load on their servers. Michele at Blacknight has been somewhat critical of WordPress in the past, mainly due to it’s inefficient SQL and the resultant high number of database queries required to generate even a simple page. Based on this Michele moved over to Movable Type 4, and despite a few teething problems, he seems to be quiet happy with it.

As part of their control panel, Blacknight provide the handy Installatron Script Installer, however, there is no installer available for MT, so users have to manually install it themselves. Unfortunately my upload connection is so bad, that it me takes hours to upload the approximately 20 MB of MT install files. As I use MAMP for local site development, I’d thought I’d have a go at a local install of MT.

Because of where MAMP is installed, it’s not a straightforward process, there are a couple of “gotchas” along the way. So for those of you interested in performing a local install of MT4, here’s a quick HOWTO.

Before you start, you’ll obviously need MAMP Server, and MT4. You can grab MAMP here, and MT4 here.

Part I: Getting Ready To Install Movable Type

  1. Once you have downloaded the MT4 ZIP file, extract the files to a folder of your choice.
  2. Within this folder, you should now have an MTOS-4.1-en folder.
  3. Open this folder and locate the mt-static folder.
  4. Move the mt-static folder to /Applications/MAMP/htdocs/
  5. Open a new Finder window and browse to /Applications/MAMP/cgi-bin/
  6. Create a new folder, and name it mt
  7. Move the remaining files in the MTOS-4.1-en folder to /Applications/MAMP/cgi-bin/mt/

Part II: Create a SQLite Database

Before starting the Movable Type installer, you need to create a database in which to store your blog settings, posts etc. While the MT developers recommend that you use MySQL for your database, an additional Perl Module is required otherwise MT will not recognise your MySQL server. However, as this will be a local install, SQLite should be more than enough for your needs. (If you really need it, the required module is DBD::mysql which can be found here, but be warned installing it is problematic and time consuming).

  1. Start the MAMP application and click the “Open Start Page” button, or alternatively, type http://localhost:8888/MAMP/ into your address bar.
  2. Click the link for the SQLiteManager at the top of the page.
  3. Type in the name of your database, and select Version 3.
  4. The full path to the database file also needs to be provided. In this example, I’ve called my database mt4, but you’re free to call it whatever you wish.
  5. Note that the path to your database will be /Applications/MAMP/db/sqlite/[your database name].db
  6. Click the Save button, and your database will be created.

Part III: Installing Movable Type

Now that you have all the MT4 files in place, and your database has been created, you’re ready to start the Movable Type web-based installer.

  1. Open your browser, and type in the following address: http://localhost:8888/cgi-bin/mt/mt.cgi
  2. The installer will tell you that the mt-static has either been renamed or moved and will request both the Static Web Path and Static File Path to the folder. Enter the following paths:
    • http://localhost:8888/mt-static
    • /Applications/MAMP/htdocs/mt-static
  3. Click the Begin button and the installer will confirm that the paths you have entered are correct.
  4. The next step brings us to the database connection setup page. Here is where we’ll tell Movable Type where our database is and how to connect to it.
  5. You’ll note that the only database connection available to us is the SQLite Database and once again we’re being asked for the path to the database. This is the exact same path that you entered in Step 6 of Part II above:
  6. In order to proceed, you’ll need to Test the Connection.
  7. Once MT can talk to your database, then Continue onto the next step -setting up your email.
  8. From the drop-down, select Sendmail and accept the default Sendmail path. Enter your email address and click the Send Test Email Button.
  9. The next step is to set up the admin user and password.
  10. The final step is to create your first blog!

Using .htaccess to Prevent Hot Linking

Hot linking is when another website embeds an image from your website in theirs by directly linking to the image. It’s considered bad form, as every time the image is requested by a browser it’s your bandwidth being used, and bandwidth costs money.

The good news is that you can prevent hot linking by adding the following lines to your .htaccess file:

<IfModule rewrite.c>RewriteEngine OnRewriteCond %{HTTP_REFERER} !^http://(.+.)?mysite.com/ [NC]RewriteCond %{HTTP_REFERER} !^$RewriteRule .*.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpe [L]</IfModule>

Thanks to altlab for the .htaccess rewrite rules.

More .htaccess Magic

Whilst researching ways to block trackback spam, I had a look at using .htaccess to deny access to the trackback URL for each post. Just in case you’re interested, here’s how it’s done. Add the following lines to the top of the .htaccess file in your blog directory.

 

SetEnvIfNoCase Request_URI "./trackback/" not_allowed

 

<Limit GET POST>Deny from env=not_allowed<Limit>

Using .htaccess to Block Spam Bots

Following on from my attempts to block trackback spam, here’s another way to block those pesky spam bots. This only works if your server uses Apache.

Because this only works on Apache, you may have already guessed that it uses the .htaccess file to prevent the spam bot from reaching your page. In order to block a spam bot, you’ll need to know the User Agent for the bot. The User Agent can be found from your web stats package. If you’re using WordPress, then you can use any of the available stats packages to find the user agents hitting your site. (I use FireStats and StatPress.)

Before you edit your .htaccess a word of warning:

 

Be very sure of what you’re doing.

 

Because .htaccess controls access to your website, you could find yourself locked out of your own website. If that happens, you’ll need to contact your hosting provider and ask them to make the changes for you.

Once you’re ready to edit your .htaccess file, open your preferred text editor and save a blank file as htaccess. Notice that there is no dot in front of the name. The reason for this is that most modern OSes will hide any file with a filename starting with a dot, and you’ll need to be able to find the file later on.

At the top of your new htaccess file type the following:

SetEnvIfNoCase User-Agent "^User Agent To Be Blocked" bad_bot

If you want to block more than one User Agent, then add the above line for each spam bot to be blocked.

Under this, you’ll need to add the following lines:

<Limit GET POST>Order Allow,DenyAllow from allDeny from env=bad_bot</Limit>

Save the changes you’ve just made, and fire up your preferred FTP program. Upload the file to the root directory of your website or blog using the ASCII file type. Once the file has been upload, use your FTP program to rename the file to .htaccess.

Check your website to ensure that you can access it, and you’re good to go!