Quantcast
Channel: Amagob IT Blog
Viewing all articles
Browse latest Browse all 13

Lighttpd+mod_rewrite+WordPress (3.4.2)

$
0
0

 

Lighttpd

Ok people, that was NOT easy.. I really wish WordPress would generate a list of rewrite for Lighttpd as it does for Apache when you activate Multisite. That would have been such a gain in time. But I can understand that unlike just throwing a .htaccess files in the folder of your site, modifying lighttpd.conf is a bigger deal and you need to know what you are doing..

I have WordPress Multisite and MediaWiki running on the same server in different subdirectories:
www.amagob.com/wpmu  =­> WordPress MU => /var/www/wpmu
www.amagob.com/wiki   => MediaWiki => /var/www/wiki
Using Apache, all I had to do was to put the .htaccess inside the wpmu folder and all my “re-writes” would work without affecting the wiki. Not a suggested config from WordPress but then again if you were to follow the instructions, you should be running only wordpress on a dedicated webserver. The url-rewrites affect everything. You should also be using your root domain as the main url (amagob.com). Not me, I don’t have unlimited server power. I also have other servers running in VM using other subdomain names. (For that Lighttpd is great as reverse proxy as well.)

Now, if you’ve started your search like me for “Lighttpd + Mod_rewrite + wordpress“, you’ll have a number of different results starting as far as suggestions from WPMU 2.9 or in 2007. My advice is you should find the most recent blogs. This blog is being published on November 2012 using WordPress 3.4.2. Also, you should note the type of Permalinks you are using in your wordpress multisite blogs, I’m using “Post name” for my website (and subsites) which is ” http://www.amagob.com/blog/index.php/sample-post/“.

The main URL has to belong to WordPress. It is an absolute nightmare for lighttpd mod_rewrite to have WordPress Multisite not being the root folder. So I had to get ride of the wpmu in my url.. no more http://www.amagob.com/wpmu but http://www.amagob.com. I can’t have my wiki as a subfolder of wordpress as well. I still want to separate these two folders to avoid overlap of rewrite rules. They had to stay side by side in /var/www:
/var/www/wp  => WordPress MultiSite
/var/www/wiki => Wiki

I therefore insalled a new wordpress under /var/www/wp; by default, WordPress will locate it’s main URL as http://www.amagob.com/wp. Then I removed the /wp and made it http://www.amagob.com. This made the website unavailable until I adjusted the url-rewrite. In /etc/lighttpd/lighttpd.conf, “mod_rewrite” is activated and the following added at the end of the file

$HTTP["host"] =~ ".*amagob.com" {
	server.document-root = "/var/www/wp"
}

Saved and Lighttpd restarted, WordPress found it’s root folder in /var/www/wp where all its files are and is happy again. :) . I exported the content of the blogs from my original site using the WordPress export tool I can now restore the content to the new site.

Thanking the Lord that Media Wiki doesn’t also need rewrite, i just modified the above to send all request for  *.amagob.com/wiki back to the root folder /var/www where it will find the wiki folder as normal without overlapping with any wordpress file/folder/url.

Here is the first part of the code which is in /etc/lighttpd/lighttpd.conf

$HTTP["host"] =~ ".*amagob.com" {
	  $HTTP["url"] =~ "/wiki/" {
		server.document-root = "/var/www"
		}
server.document-root = "/var/www/wp"

At this point WordPress and Wiki are fully installed on the same server. YAAAYY!!! .. Not quite :| !!!

Now I need to set up rewrite for the Multisite. Modify wp-config.php to define Multisite; log back into the wp-admin to Tools > Network setup and Install choosing subdirectories.. (http://codex.wordpress.org/Create_A_Network) .. add config to wp-config.php again,  create blogs.dir folder.. and you can’t do crap with the .Rewrite piece of code.. I have tried a few I found on the Internet and only this one worked for me:

url.rewrite-once = (
"^/(.*/)?files/$" => "/index.php",
"^/(.*/)?files/(.*)" => "/wp-content/blogs.php?file=$2",
"^(/wp-admin/.*)" => "$1",
"^/([_0-9a-zA-Z-]+/)?(wp-.*)" => "/$2",
"^/([_0-9a-zA-Z-]+/)?(.*\.php)" => "/$2",
"^/(.*)/?$" => "/index.php"
)

DON’T COPY THIS YET.. THERE ARE ERRORS, SEE BELOW..

(sorry can’t find the source, I was using private browsing to avoid having to clear the cache every few minutes and didn’t think about saving the url it’s everywhere on the Internet as well.)

The code worked great for the WordPress blog initially (considering the number of tries and retries). All subdirectory blogs came up with there themes but the wiki was dead. I imported all the xml from the previous blog. I had to go into phpmyadmin to clean all posts having /wpmu/ urls and copy over all images from the old website to the new. (special care to note the ID of the site to make sure you transfer the right media to the right blogs.dir folder). At his point I was trying to see if my images from the previous blog were working after being transfered into the new site and some were not. The main blog on the new site was using the wp-content/upload folder which didn’t really need redirection. For the subdirectory blogs, files go to wp-content/blogs.dir/%blog_id%/files/%year%/%month% while the URL is www.amagob.com/%blogname%/files/%year%/%month%. I deleted a picture from a subdirectory blog and imported a new one but still it would not show up; although, it would show up in the preview of the photo when trying to edit it.

Looking at the code, it seemed like the rewrite was not rewriting the url to the path of the correct folder, but rather passing part of the url to “blogs.php” file. (a day later I get to learn the difference between rewrite and redirect). A quick check did not see the file at the specified path. ms-blogs.php existed under wp-includes but then, ms-files.php sounded more like the correct file. After changing the rewrite rule, and confirming, ms-files.php was the right one. All images started showing up in the blogs..

The Wiki site was still under amagob.com and going through the url.rewrite-once. So I had to find a way to tell the rewrite to ignore whatever if going to /wiki/ and I used the following.
“^(/wiki/.*)” => “$1″,
I later found that the following was actually better
“^/wiki/.*” => “$0″,
Idea worked great, wiki was online and rocked.

Next Challenge came from @ergosteur : “Did you manage to get the search to work?”. :S my search wasn’t working.. :(

After doing some search, I fell on this blog. I figured that the URL for search using my permalinks needed to replace /?s=value-to-search to /index.php/search/value-to-search. Many tries and retries later, dinner, walk in the park, helping my wife with he dissertation.. I finally re-read that post reference below and things got much clearer. Rewrite modifies the URL internally in the webserver before it is handled. Redirect modifies the URL externally before it is handled. i.e, you can actually see the URL change in your browser. I tried
url.redirect = ( “^/\?s=(.*)” => “/index.php/search/$1″ )
But the page just took me back to the home page. I disabled rewrite and redirection works, but subdirectory blogs stop working. Renabled rewrite hoping to for redirect first and prevent any futher rewrite.
“^/\?s=/.*” => “$0″,
Didn’t work.. I read the whole reference page again, (I’m sure you’ll find my blog long now.. but it’s more entertaining than RTFM! ;) and found this section:
^/(.*)?$” => “/handler/$1″
It bothered me from the beginning that the last line had ( ) when not variable was used in the result and then it clicked. This whole time I didn’t need more redirections, just pass wordpress code to the handler (index.php) and it should work.. and it did. I took off the redirection rule and the additional rewrite and fixed the handler parameter. Search option now works on all blogs.

This is the final code:

 

$HTTP["host"] =~ ".*amagob.com" {
      $HTTP["url"] =~ "/wiki/" {
		server.document-root = "/var/www"
      }
      server.document-root = "/var/www/wp"
      url.rewrite-once = (
              "^/wiki/.*" => "$0",
              "^/(.*/)?files/$" => "/index.php",
              "^/(.*/)?files/(.*)" => "/wp-includes/ms-files.php?file=$2",
              "^(/wp-admin/.*)" => "$1",
              "^/([_0-9a-zA-Z-]+/)?(wp-.*)" => "/$2",
              "^/([_0-9a-zA-Z-]+/)?(.*\.php)" => "/$2",
              "^/(.*)/?$" => "/index.php/$1"
      )
}

 

Final note

  1. NOTE: url rewriting does not work within a $HTTP[“url”] conditional. I spent some time trying and it really doesn’t work. $HTTP[“url”] is used for WIki only and no rewrite is used for it.
  2. Rewrite rules always execute before redirect rules. Doesn’t matter if you put the redirect before or after the rewrite. Used the mechanism mod_rewrite provides to pass URLs through unmangled: specify “$0″ as the rule target. The following worked for the primary blog only and failed on the subdirectory blogs..
url.redirect("^/\?s=(.*)" => "/index.php/search/$1" )()
url.rewrite-once = (
"^/\?s=.*" => "$0"
....
)

That’s it,.. lots of hours into this one.. very satisfied it works. :) was also fun refining the restore and configuration on the new server and looked for a new theme. hope you like it and you find it useful.

Important Reference for rewrite and redirect rules on lighttpd:
http://redmine.lighttpd.net/projects/1/wiki/Docs_ModRewrite

 


Viewing all articles
Browse latest Browse all 13

Trending Articles