Running .NET 3.5 App on .NET 4.5 of Windows 8 and Server 2012

There is a very easy way to run .NET 3.5 applications under .NET 4.5 (and 4.0), without having .NET 3.5 installed…

Edit the application’s app.exe.config file:

C:\WampDeveloper\WampDeveloper.exe.config

And within the “<configuration>” section, add in:

<startup>
  <supportedRuntime version="v2.0.50727"/>
  <supportedRuntime version="v4.0"/>
</startup>

The above configuration will have the system use .NET 3.5 if it finds it, otherwise it will use .NET 4 (either 4.0 or 4.5).

Whether this work depends on whether the code used in the application is compatible with the changes that were made in the newer releases of .NET runtime. Most applications will be 100% compatible.

Related Information

1. Graph of the default .NET version that shipped with the Windows OS version:
Windows .NET Versions

2. Microsoft documentation on the above configuration changes: How to Configure an App to Support .NET Framework 4 or 4.5

3. Microsoft documentation on which versions of .NET that can be installed per Windows OS version: .NET Framework Versions and Dependencies

MySQL Error “Incorrect integer value ” for column ‘name’ at row 1″

If a PHP script tries to set a string value, such as an empty string, into a MySQL integer data type or column, you will likely see this error message:

Incorrect integer value ” for column ‘name’ at row 1

In this situation, what is happening is that the script is attempting to insert or update a row with the integer entry taking on either the default value (e.g., of 0 or NULL) or the next AUTO_INCREMENT value.

But MySQL is not interpreting this action as valid due to its SQL_MODE being in STRICT mode.

In this case, to not treat this action as an error, edit my.ini and comment out (#) this line:

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

Save my.ini, restart MySQL.

Afterwards, the value of '' (empty string) will just default to whatever:

A. Is specified as the default value of the column (set when the table was created).

B. Is the underlining designated default value for the integer data type.

If this does not help, then the script needs to be searched (in your editor of choice) for the column name, and the PHP code or the SQL statements on the found lines edited to set either a proper value, or the column name removed from those statements.

Auto Correct Broken and Mis-Encoded Links to Your Site

Some percentage of the posted links on the internet to your site will contain commonly made typing mistakes that result in the visitor ending up on a 404 / “Not Found” page.

By using Apache’s mod_rewrite and the RewriteRule directive, you can easily auto-correct the majority of the badly-formed incoming links, by reforming the broken links and then redirecting to the correct URL.

A normal link to your website on another website looks like this:

<p>Some text with a <a href="http://www.example.com/page">link anchor text here</a>.</p>

There are several ways this link can be malformed.

Incorrect HTML character encoding/escaping:

http://www.example.com/page&quot;&gt;...

Two links combined:

http://www.example.com/pagehttp://other.website/path

Dots, commas, quotes, parentheses, angle quotes at end:

http://www.example.com/page.

http://www.example.com/page,


http://www.example.com/page"


http://www.example.com/page'


http://www.example.com/page)


http://www.example.com/page(


http://www.example.com/page<


http://www.example.com/page>

White-spaces at end:

http://www.example.com/page <-- a space here

Link, line break, paragraph, list tags at end:

http://www.example.com/page</a>

http://www.example.com/page<br>


http://www.example.com/page</p>


http://www.example.com/page</li>

Variations of above:

http://www.example.com/page<a>

http://www.example.com/page<a/>


http://www.example.com/page<a


http://www.example.com/page</br>

http://www.example.com/page<br />

http://www.example.com/page<p>


http://www.example.com/page<p/>


http://www.example.com/page<li>

...

Fix Broken Incoming Links

To automatically correct the above common link mishaps, place the following code into either the website’s VirtualHost or .htaccess file.

# match on some common link mishaps: link">... escaped as link&quot;&gt;abcdefg
RewriteRule ^(.*)\s*(&quot;)+(&gt;)* $1 [R=permanent,L]

# match on some common link mishaps: two links merged
RewriteRule ^(.*)\s*https?:// $1 [R=permanent,L]

# match on some common link mishaps: ending tags and variations such as <br> <br/> <br /> </br> ... <a <a> <a > </a </a> ...
RewriteRule (.*)\s*</?a\ ?/?>?$ $1 [R=permanent,L]
RewriteRule (.*)\s*</?br\ ?/?>?$ $1 [R=permanent,L]
RewriteRule (.*)\s*</?li\ ?/?>?$ $1 [R=permanent,L]
RewriteRule (.*)\s*</?p\ ?/?>?$ $1 [R=permanent,L]

# match on some common link mishaps: links ending with . , " ' ) ( > < or any whitespace character (on specific single match, with it being one or more times)
RewriteRule (.*)[\.,"'\)\(><\s]+$ $1 [R=permanent,L]

# match on some common link mishaps: multiple ending / (more than 1 ending forward slash)
RewriteRule (.*)//+$ $1/ [R=permanent,L]

Automatically Recover From MySQL Crashes and Table Errors

Repair Existing Crashed Databases and Tables

From the command line, run a general check and repair of all MyISAM and InnoDB tables:

mysqlcheck -u root -p --auto-repair --check --optimize --all-databases

The MySQL Services has to be running for “mysqlcheck” to work.

If MySQL will not start (and only in this case), run myisamchk with the path to MySQL’s data directory:

myisamchk --recover C:/WampDeveloper/Database/mysql-data-56/*/*.MYI

Auto Repair Future Crashed Databases and Tables

Edit MySQL’s my.ini file and tell MySQL to auto recover MyISAM tables as they are opened:

[mysqld]
# For MySQL 5.1
#myisam-recover=BACKUP,FORCE
# For MySQL 5.5.3+
myisam-recover-options=BACKUP,FORCE

Remove the myisam-recover option “BACKUP” if this is happening often, as the backup files will accumulate and grow in number (and the overall size becomes a problem).

InnoDB tables automatically recover after every crash, and no additional configuration is needed… Unless the InnoDB data is so corrupt that MySQL will not start.

Trace Crashes to Specific Query

If you log all the queries made, the last one before the crash will likely be the one that is causing crashes and corruption. And from it, you can trace the issue back to its origin…

[mysqld] 
general_log = 1
general_log_file = "C:/mysql-query.log.txt"

How to Redirect “/” Root URL to Another Sub-Directory or URL

A typical redirect is usually done from either:

  1. The entire website to another website:
    Redirect / https://www.example.com/
  2. Or from one specific path of a website to another location:
    Redirect /path http://www.example.com/someother/path

The limitation of Apache’s Redirect directive is that you cannot redirect just the root URL “/”, and nothing after it, to another path or location. As Redirect matches everything after the given path, whatever follows it, and a redirect from “/” is a redirect of all the website’s URLs.

Use RedirectMatch to redirect the root URL “/” to another sub-directory or URL…

RedirectMatch ^/$ http://www.example.com/another/path

Since RedirectMatch uses a regex, it can be specific with the “/” path without matching anything more.

Forcing a PDF or DOC to Open in Browser Rather Than Downloading

If you click on a PDF or DOC link on a website, your Browser will either:

  1. Download the PDF or DOC (with or without prompting a Save-As).
  2. Open the PDF or DOC via Adobe Reader or Microsoft Word – in the Browser’s window/tab.

Most visitors come to just read the docs, preferably and automatically in 1 step. They probably don’t really want to download those docs, and then go through the process of manually opening the downloaded file in Adobe or Office.

Whether the Browser opens or downloads the file depends on the:

To force the inline viewing of PDF files, edit the website’s HTTP and HTTPS VirtualHost files, and inside the VirtualHost block add in configuration:

<LocationMatch "\.(?i:pdf)$">
    ForceType application/pdf
    Header set Content-Disposition inline
</LocationMatch>

As an alternative, to rather force downloads (as opposed to inline viewing/opening), use:

<LocationMatch "\.(?i:pdf)$">
    ForceType application/octet-stream
    Header set Content-Disposition attachment
</LocationMatch>

Save file(s). Restart Apache for changes to take effect.

Result – Automatic (no download or prompt) in-browser viewing of PDF (IE 9):
inline-open-pdf

Alternative – Downloading of PDF (IE 9):
download-save-pdf

You will need to clear your Browser’s cache (files, cookies, and history – yes, everything) and close it, every time you test this. Otherwise, you’ll keep getting the previous behavior. If the old result persists because the Browser’s PDF or Office Word plugin is caching the data itself, try renaming the file.

Normally, Apache and WAMP servers (such as WampDeveloper Pro) have a MIME-type-to-file-extension association file (Config\Apache\mime.types) that sets the correct “Content-type” Header for PDF and DOC files. But sometimes, depending on the website’s configuration and where and how the PDF files are located and/or generated, the above Header configuration is required.

It is also very important to use “LocationMatch” instead of “FilesMatch” because:

  • Your PDF and other document files might be generated or transferred by PHP – which will not be detectable through Apache’s Files directives.
  • Of the way Apache applies and merges Directory, Files, and Location sections – with Location sections taking effect with precedence over the Files sections (that might be causing your current issue).

Note: Apache’s “Header” directive requires mod_headers, which is usually loaded by WAMP, otherwise…

<IfModule !mod_headers.c>
    LoadModule headers_module modules/mod_headers.so
</IfModule>

Transferring Document Files Through PHP

If you are transferring the document files through PHP, chances are the unwanted behavior is happening due to this code…

header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($fullpath));
header('Content-Disposition: attachment; filename="' . $file . '"');
readfile($fullpath);

In this case, you can either search your code-base for some partials of the above strings, and comment out or update the lines with the proper Content-Type and Content-Disposition values, or use the provided Apache header configuration to post-process and replace the PHP generated headers.

Running IIS and Apache Together On the Same Server at the Same Time

It is possible to run both IIS and Apache on the same Windows machine, both using port 80 (http) and 443 (https), without any start-up or conflict issues.

To do this, you simply configure IIS and Apache to use different interfaces (i.e., IP addresses) with: one web-server binding on the Public IP address, and the other web-server binding on 127.0.0.1 (local host). Depending on your situation and needs, you can also assign the LAN IP to either IIS or Apache (…just ask yourself which web-server do you want reachable on the local/internal network?).

This way you’ll have both web-servers, and their hosted websites, using standard ports (with no need to use non standard URLs), just on different IPs.

With this method (for example), you could keep your existing IIS websites in production, while at the same time developing on and/or migrating to Apache (which is accessible on the local host). Later, you can move Apache to production by re-binding it.

1. Rebind all IIS websites (including “Default Web Site”) from “*” and “0.0.0.0” (“0.0.0.0″ means all IPs the system has) to the Public IP address… Select each website one by one, and in the Action pane, click Bindings.

2. Bind Apache from 0.0.0.0 to 127.0.0.1 per these simple instructions…
Binding Apache to a Specific Or Secondary IP Address

Note that when you install a WAMP server such as WampDeveloper Pro, it will stop the IIS Service and change its “Startup Type” from “Automatic” to “Disabled”…

To minimize production downtime, after WAMP installation –

  1. Stop Apache.
  2. Go into Services (run services.msc), select the IIS Service (W3SVC / “World Wide Web Publishing Service”), change its startup type back to “Auto”, and start it.
  3. Rebind all IIS websites to the public IP address, and restart IIS.
  4. Rebind Apache to 127.0.0.1 per the above instructions, and start it.