How to convert a .htaccess file for use in your Web.Config file on Windows Server

 If you're running a Windows Server and you have to run PHP websites on it chances are that you will be asked to apply rules from a .htaccess file to one of the websites on your server. Windows Server does not use .htaccess files. These usually come from Apache servers. However, Windows Servers use a similar file called the Web.Config file located at the root of all your websites hosted on your Windows Server.

You'll be glad to know that there is a converter built into IIS 7 that will convert any .htaccess rules into the format required for the Web.Config file. To convert your .htaccess file simply follow these instructions:

  1. Log onto your server and open IIS.
  2. Double click on the 'URL Rewrite' icon from the list under 'IIS'.
  3. Click on 'Import Rules...' from the right side bar.
  4. Copy the htaccess contents supplied to you and paste them into the 'Rewrite Rules' text box. Once pasted you should notice the 'Converted Rules' area will update. If you click on the XML tab you will see the Web.Config format of the rule entered.
  5. Ensure you're looking at the Tree tab and right click on the 'rule name' tag in the Tree View and click on 'Rename' to rename the rule to something other than 'Imported Rule 1' so it will make more sense to anyone looking at the web.config file later on.
  6. Click on 'Apply' from the right side bar and your rule will be added to your web.config file.

That's it. You're all done! Nice and easy.

Files uploaded through Wordpress on Windows Server are missing permissions

When uploading files in Wordpress, PHP uses a temporary upload folder. By default PHP will use the C:\Windows\Temp when uploading files. If you install PHP and Wordpress onto your Windows server then you might notice that any files uploaded through Wordpress are not accessible on your site. This is down to PHP taking the folder permissions from the C:\Windows\Temp folder which does not have public access granted on it (for obvious security reasons!).

To work around this issue you need to create a new folder and give that folder the correct permissions for the uploaded files to be available to users who visit your site.

Step 1: Create a new folder. I decided to use a folder on the root level: C:\TempUploadedFiles

Step 2: Right click on this folder and grant enough permissions for the files to be viewed online. Typically you would add IIS_IUSR, Users and NetworkService users with read permissions.

Step 3: Now we need to tell PHP to use this new folder when uploading files. Open your PHP.ini file in notepad (usually in C:\Program Files (x86)\PHP) and locate the line with 'upload_tmp_dir'. Replace the whole line with this:
upload_tmp_dir = C:\TempUploadedFiles
(make sure there is no semi colon at the start of the line!)

Gotcha: Be sure to check that there is no other 'upload_tmp_dir' declared in the PHP.ini file. In the file I was using there was another instance of 'upload_tmp_dir' located at the end of the file and it was overwriting my values.

Step 4: Save your PHP.ini file and open IIS. Locate your website and click on restart for the new settings to take effect.

Step 5: Open Wordpress and upload your new file. PHP will use your new destination to handle the file during upload and when the file has fully uploaded it will transfer it to the correct location in your wp-content/public folder with the correct permissions.

You only need to make this change once as in future all PHP sites will use the new C:\TempUploadedFiles folder.

Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine

I was working recently on an import script that would allow the end user upload an excel file, read it and import the data. Everything worked great on my own computer but when I ran the project on our Windows Server 2003 the project would fail when attempting to open the .xlsx file. The error returned to me was:

Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine

Obviously the server was missing the component required to read in a xlsx excel file. To solve this issue you need to download the 2007 Office System Driver: Data Connectivity Components onto your server. No server restart is required to install this tool and once it is installed everything will work as expected and your .net project will be allowed open and read .xlsx files.

How to create a symlink or symbolic link on Windows Server 2008 to help share folders

A symlink or symbolic link can be easily created in Widows Server 2008 to share a common folders or files amongst projects. Lets assume you have a folder in domain1.com called CSS and you want to share this folder with domain2.com. You could just upload the CSS folder into domain2.com but then you need to FTP and update the CSS folder in 2 places. If both sites are using the exact same CSS this could cause you issues over time as you might forget to roll out updates from one site to another.

Using Windows Server 2008 you could create a symlink to solve this issue. Once you have the CSS folder created on domain1.com then you can run the following command from the command line on the server to create a symlink:

MKLINK /D C:\inetpub\wwwroot\domain2.com\CSS C:\inetpub\wwwroot\domain1.com\CSS

The format of the above is: MKLINK [new directory] [where to generate the new folder] [target folder you want the new link to go to]

If you're using Windows Server 2003 you can achieve something similar using:

fsutil hardlink create c:\foo.txt c:\bar.txt

There is a good answer on serverfault.com that covers this topic quite well - http://serverfault.com/questions/7109/how-do-i-create-a-symbolic-link-in-windows