Idiosynchracy With Apache Rewrites

Posted on September 22, 2009, under GNU/Linux.

Did you know that it’s not possible to apply an Apache rewrite condition to multiple rewrite rules? I thought this would work, but requests from every IP address were being redirected:

RewriteCond %{REMOTE_ADDR} ^10\.0\.10\.10$
RewriteRule ^uploads/(.*)$ /wp-content/uploads/$1
RewriteRule (.*) http://deadorange.com/blog$1 [L,R=301]

It turns out that RewriteCond directives only apply to the following RewriteRule. So my rewrite directives above were equivalent to “If the request came from 10.0.10.10, rewrite /uploads/.* to /wp-content/uploads/.* .Next, redirect everyone to http://deadorange.com/blog$1 .”

Unfortunately, the only solution is to repeat the condition:

RewriteCond %{REMOTE_ADDR} ^10\.0\.10\.10$
RewriteRule ^uploads/(.*)$ /wp-content/uploads/$1
RewriteCond %{REMOTE_ADDR} ^10\.0\.10\.10$
RewriteRule (.*) http://deadorange.com/blog$1 [L,R=301]

That isn’t very DRY, but if it’s the only way, we have to live with it!

Importing large WordPress blogs

Posted on September 17, 2009, under GNU/Linux.

I just finished importing a WordPress blog with >1500 posts into a different WordPress blog. Importing the posts succeeded, but the last few steps at the end failed, like recalculating how many posts are in each category. This happened because the page always timed out; the import took more than 10 minutes.

If you find yourself with this problem, the fix is easy:

  1. First, comment out the following line in wp-admin/import/wordpress.php . In WordPress 2.8.4, it’s line 367.
    set_time_limit( 60 );
  2. Configure your web server to allow PHP scripts to execute for a long time. In Apache, you do that with the “Timeout” directive:
    Timeout 6000

    This can be put within a specific virtual host, or configured globally.

  3. Increase PHP’s max execution time. This is done with the “max_execution_time” setting in the appropriate php.ini :
    max_execution_time = 600    ; Maximum execution time of each script, in seconds

    or within your web server’s configuration, if that’s allowed. For example, within Apache, you include this globally, or within a virtual host:

    <IfModule mod_php5.c>  php_value max_execution_time 6000</IfModule>